1

I have two UITableView and I want to use the same custom cell for both UITableView. The problem is only one of them works fine. Only the first table display rows.

When I debug I see the cell.lb_subject is nil for the second table.

The MyCustomCell was defined as prototype cell(Is NOT a xib) only on the first table, because I don't see the point to define the same row twice.

- (UITableViewCell *)tableView:(UITableView *)mytableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

        static NSString *simpleTableIdentifier = @"MyCustomCell";

        MyCustomCell *cell = [mytableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

        if (cell == nil) {
            cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        }
        MyRowData* data = [tableData objectAtIndex:indexPath.row];
        cell.lb_subject.text = data.subject;
  • I have two UIViewController
  • One table per each UIViewController
  • I have not Xib, I have a storyboard.
  • I've uploaded to my repo a demo
Ricardo
  • 7,921
  • 14
  • 64
  • 111
  • I'm unclear what you mean by "The MyCustomCell was defined as prototype cell only on the first table, because I don't see the point to define the same row twice." Do you mean "I only set the class of the prototype cell for the first table in Interface Builder", or "I only invoked `registerClass:forCellReuseIdentifier:` for the first table", or something else? – Palpatim Dec 21 '16 at 23:58
  • @Palpatim The first one. – Ricardo Dec 22 '16 at 00:02
  • Try defining the class of the prototype cell in the second table in interface builder. You're not really defining the same row, you're telling the system "use this class as the implementation of the prototype row for the table in this scene." – Palpatim Dec 22 '16 at 00:03
  • But I have to design the same cell twice, and make connections of iboutlets, right? – Ricardo Dec 22 '16 at 00:10
  • Sorry, I conflated prototype cell in storyboard with nib/xib cell. My usual method is to define a nib/xib and use that as the single point of control, as described in http://stackoverflow.com/questions/26342151/reuse-cell-from-another-table-vc-in-same-storyboard – Palpatim Dec 22 '16 at 00:31
  • You getting Tablecell nil because it is not getting registered with every call of `cellForRow` so, register your custom cell in `viewDidLoad` instead of `cellForRow`. – dahiya_boy Dec 22 '16 at 05:58
  • Can I post a Swift answer ? – Umair Afzal Dec 22 '16 at 09:20
  • I have explained it [here](http://stackoverflow.com/questions/40275727/is-it-possible-to-create-one-tableviewcell-that-can-be-used-in-multiple-table-co/40277758#40277758), Hope i will be helpful – Umair Afzal Dec 22 '16 at 09:28
  • @UmairAfzal sure. – Ricardo Dec 22 '16 at 09:30
  • I've uploaded to my repo a demo https://github.com/rchampa/iOS-ReusePrototypeCell – Ricardo Dec 22 '16 at 10:23

5 Answers5

0

Follow this step.

Your ViewController

ViewController.h

NSMutableArray *tableData;

ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    tableData = [[NSMutableArray alloc]initWithObjects:@"test1",@"test2",@"test3",nil];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tableData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //Here MyCustomCell is identifier for both tableview cell.
    static NSString *simpleTableIdentifier = @"MyCustomCell";

    //tblCell is your custom tableview cell.
    tblCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[tblCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    cell.lb_subject.text = [tableData objectAtIndex:indexPath.row];
    return cell;
}

Your Custom cell

tblCell.h

//Here lb_subject is common Outlet for both label of both cell.
@property (strong, nonatomic) IBOutlet UILabel *lb_subject;
amit_donga
  • 224
  • 1
  • 9
0

Give Tag to your both tables and then use dequeueReusableCellWithIdentifier for both tables separately.

 if (tableView.tag==1) {

    static NSString *simpleTableIdentifier = @"MyCustomCell";

        MyCustomCell *cell = [mytableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

        if (cell == nil) {
            cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        }
        MyRowData* data = [tableData objectAtIndex:indexPath.row];
        cell.lb_subject.text = data.subject;
        return cell;
    }
    else {
    static NSString *simpleTableIdentifier = @"MyCustomCell";

        MyCustomCell *cell = [mytableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

        if (cell == nil) {
            cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        }
        MyRowData* data = [tableData objectAtIndex:indexPath.row];
        cell.lb_subject.text = data.subject;
        return cell;   
}
Niharika
  • 1,188
  • 15
  • 37
  • I have never used tag before. How it works using only integers?. I'm confuse because in android you can save objects on it. – Ricardo Dec 22 '16 at 07:31
  • I dont have any idea about android. In ios you can set tag for your table from interface builder or from your code it self. it works using integers. – Niharika Dec 22 '16 at 07:34
  • I see. But cell.lb_subject is still. And i wonder why do you think tag could works. – Ricardo Dec 22 '16 at 07:42
  • It worked for me in few cases. firstly check if you are getting the cell object for your second table? – Niharika Dec 22 '16 at 08:12
  • But i don't understand where you set the value of tag. And why you check tableView.tag==1 – Ricardo Dec 22 '16 at 09:25
  • In your interface where you have taken a table, there will be a option of setting tag. Select your table -> On the left side choose 4th tab (Show the attribute inspector) -> In that go to the view section it is having tag feature, you can give tag value to your table from there. – Niharika Dec 22 '16 at 09:57
  • Yes i will upload a demo to github within a few minutes. – Ricardo Dec 22 '16 at 10:14
  • I've uploaded to my repo a demo https://github.com/rchampa/iOS-ReusePrototypeCell – Ricardo Dec 22 '16 at 10:24
0

You've not added cell in other table if you do not want to create another prototype cell for viewController 2 then you need to use XIB

Register your XIB :

Register your customcell in ViewDidload instead of cellForRow

[self.tableView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"CustomCell"];

and,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CustomCell"];
    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        cell = (CustomCell *)[topLevelObjects objectAtIndex:0];
    }

    cell.cellLabel.text = _arrData[indexPath.row];

    return cell;
}

If you still face same problem then let me know

dahiya_boy
  • 9,298
  • 1
  • 30
  • 51
  • Now i'm getting an exception: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle <.../data/Containers/Bundle/Application/XXXX/Myapp.app> (loaded)' with name 'MyCustomCell'' – Ricardo Dec 22 '16 at 07:40
  • Did you re-check the spelling of xib class and cell identifier.?? – dahiya_boy Dec 22 '16 at 08:51
  • Yes I did. There are not typos. – Ricardo Dec 22 '16 at 08:52
  • I edited my `cellForRow` try this one and please check your cell identifier bcz i write `CustomCell` and you write `MyCustomcell`. – dahiya_boy Dec 22 '16 at 08:55
  • Can you show screenshot of your xib? Because this piece of code is working in my x-code. – dahiya_boy Dec 22 '16 at 08:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/131227/discussion-between-ricardo-and-tinu-dahiya). – Ricardo Dec 22 '16 at 08:58
0

I would like to register the cell with the tableView in the class of customCell. something like this

class func cellForTableView(tableView: UITableView, atIndexPath indexPath: NSIndexPath) -> YourCustomTableViewCell {
let kYourCustomTableViewCellIdentifier = "kYourCustomTableViewCellIdentifier"
tableView.registerNib(UINib(nibName: "YourCustomTableViewCell", bundle: NSBundle.mainBundle()), forCellReuseIdentifier:     kYourCustomTableViewCellIdentifier)

let cell = tableView.dequeueReusableCellWithIdentifier(kYourCustomTableViewCellIdentifier, forIndexPath: indexPath) as! YourCustomTableViewCell

return cell
}

You can see a more detailed answer Here.

Community
  • 1
  • 1
Umair Afzal
  • 4,947
  • 5
  • 25
  • 50
0

I am guessing that you are having two prototype cell for each of your table.

Let's say for, table A - "CustomCellA" & for table B - "CustomCellB"

both customCell's parent class should be same.

Now in cellForRowAtIndexPath write below code,

MyCustomCell *cell;

if (tableView == tableViewA) 
    cell = [mytableView dequeueReusableCellWithIdentifier:@"CustomCellA"];
else
    cell = [mytableView dequeueReusableCellWithIdentifier:@"CustomCellB"];

Your cell's Outlet and attributes should be find for both tableview.

Do let me know the results.

Thanks, Hope this helps.

Dhruvik
  • 984
  • 4
  • 18
  • I have one storyboard. I only have one prototype cell define on Table A. Table A load it cells and display results fine. Table B try to load the same cell but it loads with all its fields as nil. – Ricardo Dec 22 '16 at 09:50
  • you have to define same prototype cell with different identifier for table B also. Could you share your code, how u define prototype for Table A? you using xib for cell? @Ricardo – Dhruvik Dec 22 '16 at 10:02
  • Yes i will upload a demo to github within a few minutes. – Ricardo Dec 22 '16 at 10:13
  • I've uploaded to my repo a demo https://github.com/rchampa/iOS-ReusePrototypeCell – Ricardo Dec 22 '16 at 10:23
  • it's done. you have to add prototype in your BViewController's tableview. Give it name as CellB or what ever you like you can give same as another prototype identifier, then in BViewController's tableview cellforrowatindexpath change to "CellB" i.e. [tableView dequeueReusableCellWithIdentifier:@"cellB"] @Ricardo – Dhruvik Dec 22 '16 at 10:32
  • But I have to design it again? – Ricardo Dec 22 '16 at 10:34
  • yes you have to copy that cell again , it is because both tableview's parent controller are different in story board. Protoype cell is belongs to particular tableview only. – Dhruvik Dec 22 '16 at 10:35