将排序列表项与UItableview标题的字符串进行比较

时间:2012-06-24 18:02:49

标签: iphone ios uitableview sorting compare

我在表格视图中对列表进行了排序,但我不想将列表中的项目显示为行的标题。我需要将排序列表的第一项与字符串进行比较,如果它是相同的,我想显示另一个字符串作为标题。

举个例子,更好地解释一下:

if (first item of the sorted list == some string) {

   cell.textlabel.text = @"name";

}

是否可以做这样的事情?

2 个答案:

答案 0 :(得分:0)

您的排序数组是否存储在NSArray中?如果是这样,您可以使用:

if ([[sortedArray objectAtIndex:0] isEqualToString:@"Some String"]) {
    cell.textlabel.text = @"name";
}

答案 1 :(得分:0)

如果我正确地解释了这个问题,那么是的。如果第一个项目是一个单元格,那么可能就是这样,那么你就可以做到;

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
UITableViewCell *firstCell = [yourTableView cellForRowAtIndePath:indexPath];
NSString *titleOfFirstCell = firstCell.textLabel.text;

然后你可以比较它以检查它应该更改为什么 - 我将把这个字符串称为'updatedTitle'。

titleOfFirstCell.textLabel.text = updatedTitle;
[firstCell setNeedsDisplay];

希望这有帮助,

乔纳森