我有UITableViewController
其中包含項目列表。現在,我希望列表在視圖出現後自動滾動到一個項目(索引= bestOne
)。同時,我希望將該物品塗成紅色並標記爲Marked
。以編程方式滾動並在運行時着色UITableViewCell
我的代碼粗略地實現了我想要的。但是,我實際上看到不止一個紅色物品迭代:每10個物品都有一個紅色物品。
我對iphone開發相當陌生,我認爲它可能與可重用的單元格有關。但我不確定爲什麼。任何人都可以提出一種解決這個問題的方法嗎?提前致謝。
(void)viewDidAppear:(BOOL)animated
{
if (self.bestOne != -1)
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.bestOne inSection:0];
[self.tableView scrollToRowAtIndexPath: atScrollPosition: animated:YES];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [self.array objectAtIndex:indexPath.row];
if (indexPath.row == self.bestOne)
{
cell.detailTextLabel.text = @"Marked";
cell.textLabel.textColor = [UIColor redColor];
}
return cell;
}
不要忘記重置'textLabel.textColor'! – thelaws 2011-05-17 04:33:46
它的工作原理。我沒有想到它會那麼簡單。謝謝。 – Zeiga 2011-05-17 04:36:02
@Zeiga,@thelaws是對的。我沒有添加'cell.textLabel.textColor = [UIColor blackColor];',因爲我誤以爲它是'detailTextLabel'。你也應該添加它。 – 2011-05-17 04:41:26