以下代碼設置單元格文本並僅爲上次選中的添加校驗標記。總是隻有一個單元格檢查被標記並且正常工作,除了第一次顯示時。因此,直到您按任何其他單元格時,文本纔會顯示在該單元格中(僅限那個單元格)。例如,如果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