开发者

UITableView selected cell not displaying text when started

开发者 https://www.devze.com 2023-03-30 04:51 出处:网络
following code sets cell texts and add a checkMark only to last selected. Always only one cell check marked and works properly excepting when it is displayed for first time. So, text is not showed for

following code sets cell texts and add a checkMark only to last selected. Always only one cell check marked and works properly excepting when it is displayed for first time. So, text is not showed for that cell (only that one) until you press any other cell. For example, if cellPos = 4 when viewDidLoad, that cell will not contain text.

- (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];
}


Have you tried moving the if-else block after the switch block? This way you set the text of the cell before you set the cell to selected. The fact that it only occurs the first time suggests to me that it may be an order of operations problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消