Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a custom UITableViewCell

I'm trying to create a custom UITableViewCell.

From XCode 4.6 interface builder, I've set the Style property of the cell to Custom. And added controls to the cell using drag and drop. 2 UILables and a UIButton. It looks like this.

enter image description here

I created a separate class deriving from UITableViewCell to assign the properties of the 3 UI elements and make the changes there. I've set the cell's custom class as DashboardCell from the Identity Inspector as well.

DashboardCell.h

#import <UIKit/UIKit.h>

@interface DashboardCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *numberOfMails;
@property (weak, nonatomic) IBOutlet UILabel *mailType;
@property (weak, nonatomic) IBOutlet UIButton *numberOfOverdueMails;

@end

DashboardCell.m

#import "DashboardCell.h"

@implementation DashboardCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.numberOfOverdueMails setBackgroundColor:[UIColor colorWithRed:244/255.0f green:119/255.0f blue:125/255.0f alpha:1.0f]];
        [self.numberOfOverdueMails setTitle:@"lol" forState:UIControlStateNormal];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

In the TableViewController, I have modified the following method to return my custom cell.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    DashboardCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    if (cell == nil) {
        cell = [[DashboardCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    return cell;
}

My problem is even though the custom button shows up, the changes I've done (changing the background color of the button, changing the title of one UILabel) aren't showing up. What seems to be the mistake here?

like image 514
Isuru Avatar asked Feb 28 '13 04:02

Isuru


People also ask

How do you create a table view cell subclass?

The same way that you create a UIViewController or UITableViewController : In your project, go to New File. Select Cocoa Touch Class under iOS. The subclass is UITableViewCell and the name is whatever name you're giving it. Within that class, you drag your buttons, labels, etc to create your outlets.

How do I add a prototype cell to UITableView?

First, design your table cell on Storyboard whatever you want. Now, make a subclass of UITableViewCell and assign this class to your prototype custom cell which you have designed on Storyboard. Also, don't forget to set "reuse identifier in Storyboard table cell.


2 Answers

The method initWithStyle:reuseIdentifier: will not be called because you're using interface builder to create a cell.

You can set the background color and title by overriding the method awakeFromNib.

You can also set these in the method tableView:cellForRowAtIndexPath:

like image 185
Oscar Avatar answered Nov 15 '22 06:11

Oscar


If you get your cell from a xib or storyboard, dequeueReusableCellWithIdentifier:forIndexPath: will always return a cell -- if one exists it will reuse it, if not it will create one from the template in IB. Therefore, your if(cell ==nil) clause will never be satisfied, and in fact is no longer needed. If you want to use an init method, then use initWithCoder:

like image 40
rdelmar Avatar answered Nov 15 '22 08:11

rdelmar