werbary
werbary

Reputation: 1095

custom UITableViewCellStyle

I want to make custom uitableviewcellstyle to make comments in my app. I want uitableviewcell with comment text, number of likes, author's name, date etc... Have you any ideas? I've created method, but I don't know how realize it. My code:

- (UITableViewCell *)getCommentTableCellWithTableView:(UITableView *)tableView commentText:(NSString *)commentText numberOfRows:(NSInteger)numberOfRows numberOfLikes:(NSString *)numberOfLikes date:(NSString *)date writer:(NSString *) writerName {
    static NSString *CellIdentifier = @"TitleCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];



    return cell;
}

Upvotes: 0

Views: 6004

Answers (2)

Daniel Galasko
Daniel Galasko

Reputation: 24237

For those of us using the newer (iOS 6 and up) UITableView API for dequeueing cells namelydequeueReusableCellWithIdentifier:forIndexPath this is actually guaranteed to return an instantiated cell so we can't perform the nil check and manually call initWithStyle. Therefore the best solution is to subclass UITableViewCell and manually enforce the style in initialisation.

So as an example if we wanted a cell with the UITableViewCellStyleSubtitle style we would create a custom subclass:

@interface ABCSubtitledTableViewCell : UITableViewCell

@end

@implementation ABCSubtitledTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    return [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
}

@end

And then in our viewDidLoad we would register the appropriate class

[tableView registerClass:[ABCSubtitledTableViewCell class] forCellReuseIdentifier:NSStringFromClass([ABCSubtitledTableViewCell class])];

Making our dequeue method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ABCSubtitledTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ABCSubtitledTableViewCell class]) forIndexPath:indexPath];
    cell.textLabel.numberOfLines = 0;
    cell.textLabel.text = @"Hello";
    cell.detailTextLabel.text = @"World";
    return cell;
}

Upvotes: 0

Kjuly
Kjuly

Reputation: 35131

I'm sorry I can't find a tutorial with clear steps, but you can search some related posts or questions on this site.
Hope the simple code below could help you.

Here is a doc may helps too, take your time to have a look ;)
http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/TableView_iPhone/TableViewCells/TableViewCells.html


New class inherit from UITableViewCell, CustomCell.h:
(Tips:File->New File->Objective-C class->set class name & choose the subclass UITableViewCell)

@interface MapsListViewCell : UITableViewCell
{
    // Add iVars that you want.
}
// Some custom methods

CustomCell.m:

// ...
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}
// ...
// Some custom methods
- (void)setAuthorName:(NSString *)name
{
    // ...
}

TableViewController.m:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[CategoriesListViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    CategoriesListViewCell * customCell = (CategoriesListViewCell *)cell;
    // Set your data:
    [customCell setAuthorName:@"God"];
    // ...etc.

    return cell;
}

Upvotes: 3

Related Questions