GoldFire
GoldFire

Reputation: 53

How can i create a custom UITableViewCell?

I want to know how can i create a custom UITableViewCell with three parts in the UITableViewCell like that:

Screenshot

Upvotes: 1

Views: 2000

Answers (1)

Jano
Jano

Reputation: 63707

Create a blank XIB with a UITableViewCell and design the cell dropping elements on it. Set arbitrary tags (1,2,3...) on each element so you can retrieve them later.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    NSString *identifier = @"xxx";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        NSArray *nibObjects = [[NSBundle mainBundle] loadNibNamed:@"NameOfTheBlankXIB" owner:self options:nil];
        cell = [nibObjects objectAtIndex:0];            
    }

    // get the elements inside the cell by tag and set a value on them
    UILabel*label = (UILabel*)[cell viewWithTag: 1];
    label.text = @"some text";

    return cell;
}

Another way is to go to the NIB and set this view controller as the file owner and hook the the cell to a ivar of the view controller, like IBOutlet UITableViewCell *tableViewCell.

Now loading this with this instance as the owner automatically hooks the cell to the outlet tableViewCell.

    [[NSBundle mainBundle] loadNibNamed:@"NameOfTheBlankXIB" owner:self options:nil];
    // at this point the cell is hooked to the ivar so you get a reference to it:
    cell = self.tableViewCell;

Upvotes: 6

Related Questions