UITableViewController accessing static cells programmatically emits

Let's say I have a table with 10 static cells in it, is there a way to programmatically select a specific cell?

I tried this

UITableViewCell *cell = [self.tableView.subviews objectAtIndex:indexPath.row];

      

but that doesn't actually return the table cell.

this seems to break my code

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

      

I am trying to set individual heights for static cells in code. The parameter would have to do outputs for each individual static cell, but that seems silly.

+3


source to share


6 answers


To access statically generated cells try this:

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

      

This works for static cells. So, if you are in ...

- (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 whatever you want with a "cell".

I had a ViewController that had two UITableViews on it. One had cells defined statically from the storyboard, and the others had cells defined dynamically using code. Considering that I was using the same ViewController as a delegate for both tables, I needed to prevent new cells from being created where cellForRowAtIndexPath was called where cells were already created.

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

Good luck.

+14


source


Create @IBOutlet

.



This will work even if you set up your static cells programmatically.

+5


source


You can try this ...

UITableViewCell *cell = (UITableViewCell*)[yourTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:rowvalue inSection:0]];

      

+1


source


If you need access to a cell object, then using the UITableViewCell cellForRowAtIndexPath method is appropriate.

This can either just pass the cell, if visible, or call the delegate method cellForRowAtIndexPath (don't mix them up) that you must provide. If this is one of them, then dig deeper and investigate the root cause of the crash.

+1


source


Use method table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
     NSInteger height;  
     if(0 == indexPath.row)  
       {
          height = 44;
       }
     else
      {  
        height = 50;
      }
   return height;
}

      

0


source


This is Swift 2.3. Decision.

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


source







All Articles