如何创建自定义UITableViewCell?

时间:2022-09-25 18:44:14

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

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

如何创建自定义UITableViewCell?

1 个解决方案

#1


6  

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创建一个空白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;
}

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.

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

Now loading this with this instance as the owner automatically hooks the cell to the outlet 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;

#1


6  

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创建一个空白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;
}

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.

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

Now loading this with this instance as the owner automatically hooks the cell to the outlet 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;