didSelectRowAtIndexPath选择除选定单元格之外的另一个单元格

时间:2013-09-19 09:40:19

标签: ios uitableview

在我的代码中,当我在表格视图中选择一个单元格时,它也会选择另一个单元格。 任何人都可以帮忙解决这个问题。

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // Configure the cell...
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    // Display recipe in the table cell
    Player *player = [players objectAtIndex:indexPath.row];

    UILabel *playerIdLabel = (UILabel *)[cell viewWithTag:104];
    playerIdLabel.text = player.pid;

    UIImageView *playerImageView = (UIImageView *)[cell viewWithTag:100];
    playerImageView.image = [UIImage imageNamed:player.imageFile];

    UILabel *playerNameLabel = (UILabel *)[cell viewWithTag:101];
    playerNameLabel.text = player.name;

    UILabel *playerDetailLabel = (UILabel *)[cell viewWithTag:102];
    playerDetailLabel.text = player.detail;

    UIButton *playerAddButton = (UIButton *)[cell viewWithTag:103];

    // Assign our own background image for the cell
    UIImage *background = [self cellBackgroundForRowAtIndexPath:indexPath];

    UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background];
    cellBackgroundView.image = background;
    cell.backgroundView = cellBackgroundView;

    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        UILabel *playerNameLabel = (UILabel *)[cell viewWithTag:101];
        NSString *playerName = playerNameLabel.text;

        UILabel *playerIdLabel = (UILabel *)[cell viewWithTag:104];
        [playerIdLabel setHidden:NO];
}

2 个答案:

答案 0 :(得分:0)

尝试使用set nil作为重用标识符。

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

答案 1 :(得分:0)

我认为您可能已经减少了IB的大小,忘了使用委托方法设置高度。试试这个

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    return 30.0;
}