UITableViewController accessing static cells programmatically issue

后端 未结 6 1414
孤独总比滥情好
孤独总比滥情好 2021-01-05 23:41

Say I have a table with 10 static cells in it, is there a way to select a certain cell programmatically?

I\'ve tried this

UITableViewCell *cell = [se         


        
相关标签:
6条回答
  • 2021-01-06 00:02

    This is a Swift 2.3. Solution.

    The UITableViewController is created in IB.

    /*
        NOTE
        The custom static cells must be
        In the IB tableview if one is being used 
        They also must be updated to be MYCustomTableViewCell 
        instead of UITableViewCell
    */
    import UIKit
    
    class MYCustomTableVC: UITableViewController
    {
        override func viewDidLoad()
        {
            super.viewDidLoad()
            // NO Nib registration is needed
        }
    
        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
        {
            let cell = super.tableView(tableView, cellForRowAtIndexPath: indexPath) as! MYCustomTableViewCell
            // MYCustomTableViewCell can be created programmatically 
            // without a Xib file 
            return cell
        }
    }
    
    0 讨论(0)
  • 2021-01-06 00:15

    You can try this...

    UITableViewCell *cell = (UITableViewCell*)[yourTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:rowvalue inSection:0]];
    
    0 讨论(0)
  • 2021-01-06 00:16

    Use table view delegate method

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
         NSInteger height;  
         if(0 == indexPath.row)  
           {
              height = 44;
           }
         else
          {  
            height = 50;
          }
       return height;
    }
    
    0 讨论(0)
  • 2021-01-06 00:21

    If you need accessing the cell object, then using UITableViewCell method cellForRowAtIndexPath is quite appropriate.

    That may either just pass the cell, if it is visible, or call the delegate method cellForRowAtIndexPath (do not mix them up) which you should provide. If that one crashes then dig deeper and investigate the root cause of the crash.

    0 讨论(0)
  • 2021-01-06 00:24

    To access statically created cells, try this:

    UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
    

    This works for static cells. So, if you're in the...

    - (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
         UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
    
        return cell;
    
    }
    

    ... delegate, you can access all statically configured cells using the above declaration. From there, you can do what ever you want with "cell".

    I had a ViewController that had two UITableViews on it. One of them had cells defined statically, with a Storyboard, and the other had cells defined dynamically using code. Given I was using the same ViewController as delegate for both tables, I needed to prevent new cells from being created where cellForRowAtIndexPath was being called where cells had already been created.

    In your case, you need to gain programmatic access to your cells.

    Have fun.

    0 讨论(0)
  • 2021-01-06 00:24

    Create an @IBOutlet.

    This will work even if you re-arrange your static cells programmatically.

    0 讨论(0)
提交回复
热议问题