简体   繁体   中英

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:

屏幕截图

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM