Objective c UITableView:如何利用单元重用标识符?

Objective c UITableView:如何利用单元重用标识符?,objective-c,uitableview,ios4,Objective C,Uitableview,Ios4,我的书告诉我应该像这样使用UITableView单元的重用标识符 //check for reusable cell of this type UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"]; //if there isn't one, create it if(!cell){ cell = [[UITableViewCell alloc] initWit

我的书告诉我应该像这样使用UITableView单元的重用标识符

//check for reusable cell of this type
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"]; 

//if there isn't one, create it
if(!cell){
    cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault
                      reuseIdentifier: @"UITableViewCell"]; 
}   
所以从我所看到的,它检查我们想要的细胞类型是否存在,如果存在,它使用它,但如果不存在,它会创建一个具有所需标识符的细胞


如果我们有多个单元样式(即不同的重用标识符),我们将如何使用它为我们创建不同的可重用单元

表视图管理单独的单元格队列,以便为每个标识符重用。例如,如果单元格对于偶数行和奇数行应该有不同的外观(如示例所示),您可以这样做

NSString *cellIdentifier = (indexPath.row % 2 == 0 ? @"EvenCell" : @"OddCell");
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault
                      reuseIdentifier:cellIdentifier];
    if (indexPath.row % 2 == 0) {
        // set cell properties for even rows
    } else {
        // set cell properties for odd rows
    }
}
使用不同的重用标识符可以确保不重用偶数行中的单元格 作为奇数行的单元格


(此示例仅在不插入或删除单元格的情况下有效。另一个示例是不同的单元格,具体取决于行的内容。)

表格视图管理单独的单元格队列,以便为每个标识符重复使用。例如,如果单元格对于偶数行和奇数行应该有不同的外观(如示例所示),您可以这样做

NSString *cellIdentifier = (indexPath.row % 2 == 0 ? @"EvenCell" : @"OddCell");
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault
                      reuseIdentifier:cellIdentifier];
    if (indexPath.row % 2 == 0) {
        // set cell properties for even rows
    } else {
        // set cell properties for odd rows
    }
}
使用不同的重用标识符可以确保不重用偶数行中的单元格 作为奇数行的单元格


(此示例仅在不插入或删除单元格时有效。另一个示例是不同的单元格,具体取决于行的内容。)

indexPath
,请使用它。它包含您的行和节,因此无论您想设置什么属性,都可以从行和节中选择大小写并相应地进行设置。

indepath
,使用它。它包含您的行和节,因此无论您要设置什么属性,都可以从行和节中选择大小写并进行相应设置。

然后使用不同的标识符创建单元格:)
UITableViewCell*cell1=
UITableViewCell*cell2=
,然后使用不同的标识符创建单元格:)
UITableViewCell*cell1=
UITableViewCell*cell2=
,等等。