以下代码设置单元格文本并仅为上次选中的添加校验标记。总是只有一个单元格检查被标记并且正常工作,除了第一次显示时。因此,直到您按任何其他单元格时,文本才会显示在该单元格中(仅限那个单元格)。例如,如果cellPos
= 4时viewDidLoad
,该单元格将不包含文本。UITableView选中的单元格在启动时不显示文本
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString* cellIdentifier = @"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell == nil)
{
cell = (UITableViewCell*) [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell.accessoryType = UITableViewCellAccessoryNone;
if (indexPath.section == 0) {
if(indexPath.row == cellPos)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.selected = YES;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selected = NO;
}
switch (indexPath.row) {
case 0:
cell.textLabel.text = @"English";
cell.imageView.image = [UIImage imageNamed:@"english.png"];
break;
case 1:
cell.textLabel.text = @"Español";
cell.imageView.image = [UIImage imageNamed:@"spanish.png"];
break;
case 2:
cell.textLabel.text = @"Deutsch";
cell.imageView.image = [UIImage imageNamed:@"germany.png"];
break;
case 3:
cell.textLabel.text = @"Français";
cell.imageView.image = [UIImage imageNamed:@"french.png"];
break;
case 4:
cell.textLabel.text = @"Italiano";
cell.imageView.image = [UIImage imageNamed:@"italian.png"];
break;
default:
break;}
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
cellPos = indexPath.row;
[tableView reloadData];
}
static NSString * cellIdentifier = @“cellIdentifier”; –
你在哪里设置cellPos? – MishieMoo
设置为.h并在viewDidLoad指定值时 – Ruth85