布局自定义UITableViewCell的问题

时间:2014-02-07 07:59:00

标签: ios uitableview

我使用NIB中的自定义UITableView创建了UITableViewCell。在加载视图控制器时,一切似乎都正常。但是当我开始滚动UITableView时,有些情况下中间无缘无故地出现一个空单元格。

我不知道发生了什么。我的cellForRowAtIndexPath非常简单。但是我已经为自定义UITableViewCell添加了手势。这会导致问题吗? 这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *tableIdentifier = @"FTStreamCell";
    FTStreamCell *cell = (FTStreamCell *)[tableView dequeueReusableCellWithIdentifier:tableIdentifier];
        if (cell == nil)
        {
            NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"FTStreamCell" owner:self options:nil];
            cell = [nib objectAtIndex:0];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        cell.delegate = self;
        return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    FTStreamCell *streamCell = (FTStreamCell*) cell;
    if (!indexPath.section == 0)
    {
        if ((NSNull*)[[[self.cachedData objectForKey:kQuestions] objectAtIndex:indexPath.row] objectForKey:kQuestion] != [NSNull null])
            streamCell.question.text = [[[self.cachedData objectForKey:kQuestions] objectAtIndex:indexPath.row] objectForKey:kQuestion];
        streamCell.timePosted.text = [NSString stringWithFormat:@"Today at Bangalore, India"];
        // To Show My Questions
        NSLog(@"Me %d", [[[NSUserDefaults standardUserDefaults] objectForKey:kUserId] integerValue]);
        if ([[[self.cachedData objectForKey:kQuestions] objectAtIndex:indexPath.row] objectForKey:kUserId] == [[NSUserDefaults standardUserDefaults] objectForKey:kUserId])
        {
            streamCell.mainView.backgroundColor = [UIColor colorWithRed:244.0f/255.0f green:240.0f/255.0f blue:247.0f/255.0f alpha:1];
            streamCell.userCheck = YES;
        }
        else
        {
            streamCell.mainView.backgroundColor = [UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:1];
            streamCell.userCheck = NO;
        }
        [cell layoutSubviews];
    }
}

感谢。

1 个答案:

答案 0 :(得分:0)

UITableViews重用表格单元格以减少内存分配。因此,当您开始滚动“白色单元格”时,可能是之前使用和修改过的单元格。在willDisplayCell方法中,在应用样式更改之前尝试“重置”单元格。 “重置”将是您的自定义方法,可将所有内容更改回单元格中的默认值。

相关问题