如何创建自定义UITableViewCell?

Gol*_*ire 1 iphone xcode cocoa-touch ipad

我想知道如何创建一个UITableViewCell包含三个部分的自定义UITableViewCell:

截图

Jan*_*ano 6

使用UITableViewCell创建一个空白XIB,并在其上设计单元格删除元素.在每个元素上设置任意标记(1,2,3 ...),以便稍后检索它们.

- (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;
}
Run Code Online (Sandbox Code Playgroud)

另一种方法是转到NIB并将此视图控制器设置为文件所有者,并将单元格挂钩到视图控制器的ivar,如IBOutlet UITableViewCell *tableViewCell.

现在使用此实例加载此作为所有者自动将单元格挂钩到插座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;
Run Code Online (Sandbox Code Playgroud)