UITableView - 滚动后某些值会发生变化

时间:2014-06-11 20:33:09

标签: objective-c xcode uitableview ios7 scroll

我发现了很多关于此的问题。但遗憾的是找不到我的问题的解决方案。 我的代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{


static NSString *CellIdentifier = @"DuelListCell";
DuelRequestsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

int row = [indexPath row];

cell.ReceiverName.text = _receiverFullName[row];
cell.ReceiverTeam.text = _receiverTeamName[row];

if ([_senderId[row] intValue] == [app.activeUser intValue]){

    if ([_duelStatusId[row] intValue] == 0){
        cell.ReceiverResult.hidden = YES;
        cell.ReceiverMessage.hidden = NO;
        cell.ReceiverMessage.text = @"Waiting";

        cell.ReceiverMessage.textColor = [UIColor colorWithRed:245.0/255.0 green:152.0/255.0 blue:41.0/255.0 alpha:1];

    }
    else if ([_duelStatusId[row] intValue] == 2){
        cell.ReceiverResult.hidden = YES;
        cell.ReceiverMessage.hidden = NO;
        cell.ReceiverMessage.text = @"Refused";

        cell.ReceiverMessage.textColor = [UIColor colorWithRed:245.0/255.0 green:29.0/255.0 blue:41.0/255.0 alpha:1];

    }


}else if ([_receiverId[row] intValue] == [app.activeUser intValue]){



    if ([_duelStatusId[row] intValue] == 0){

        cell.ReceiverResult.hidden = YES;
        cell.AcceptChallange.hidden = NO;

    }
    else if ([_duelStatusId[row] intValue] == 2){
        cell.ReceiverResult.hidden = YES;
        cell.ReceiverMessage.hidden = NO;
        cell.SenderMessage.text = @"You Refused";

        cell.SenderMessage.textColor = [UIColor colorWithRed:245.0/255.0 green:29.0/255.0 blue:41.0/255.0 alpha:1];
    }else if ([_duelStatusId[row] intValue] == 1){
        //NSLog([NSString stringWithFormat:@"Row: %d", row]);
    }
}

return cell;

}

当我滚动表格时,值正在变化。我尝试了许多不同的方法,但找不到解决方案,最后我很困惑。

3 个答案:

答案 0 :(得分:0)

你应该检查你的单元格是否为零,如果是,则创建一个新的单元格。

if (cell == nil) {

  cell = [[DuelRequestsTableViewCell alloc] initWithNib.....] ;
}

答案 1 :(得分:0)

使用它正在工作

static NSString *CellIdentifier = @"DuelListCell";  
DuelRequestsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
    {
        cell= [[[NSBundle mainBundle] loadNibNamed:cellNibName owner:self options:nil] objectAtIndex:0];
    }

并在此处设置单元格标识符 enter image description here

答案 2 :(得分:0)

这就是忘记if-else语句的else`结尾。