UITableView单元格从其他单元格中获取内容

时间:2012-09-23 11:16:33

标签: iphone objective-c ios xcode uitableview

我的UITableView个细胞从其他细胞中获取内容(图像)。我不知道为什么会这样,但我确信我的错误很愚蠢。在下图中,您可以看到它的外观。顶部的单元格从底部的单元格中获取图像(Facebook和Twitter图标)。

以下是 cellForRowAtIndexPath:

的代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSDictionary *dictionary = [mainArray objectAtIndex:indexPath.section];
    NSArray *array1 = [dictionary objectForKey:@"stuff"];

    NSString *cellValue = [array1 objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue; // загаловок клетки
    cell.textLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:16]; 
    cell.textLabel.textColor = [UIColor colorWithRed:145/256.0 green:0/256.0 blue:0/256.0 alpha:1.0];    
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    cell.textLabel.textAlignment = UITextAlignmentLeft;

    if (indexPath.section == 0 && indexPath.row == 1)
    {
        if([[NSUserDefaults standardUserDefaults]boolForKey:@"password"] == YES)
        {
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
    }

    switch(indexPath.section)
    {

        case 2:
        {
            cell.textLabel.textAlignment = UITextAlignmentCenter;
        }
        break;

        case 3:
        {
            if (indexPath.row == 0) 
                cell.imageView.image = [UIImage imageNamed:@"facebook"];
            else if (indexPath.row == 1)
                cell.imageView.image = [UIImage imageNamed:@"twitter"];
            else if (indexPath.row == 2)
                cell.imageView.image = [UIImage imageNamed:@"youtube"];
            else
                cell.imageView.image = [UIImage imageNamed:@"vk"];
        }
        break;
            default:
            break;
    }




    return cell;
}

enter image description here

我该如何解决?

1 个答案:

答案 0 :(得分:1)

要重复使用单元格,因此每当您不想要显示图像时,必须将单元格的imageView设置为nil。试试这个:

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    NSDictionary *dictionary = [mainArray objectAtIndex:indexPath.section];
    NSArray *array1 = [dictionary objectForKey:@"stuff"];

    NSString *cellValue = [array1 objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue; // загаловок клетки
    cell.textLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:16]; 
    cell.textLabel.textColor = [UIColor colorWithRed:145/256.0 green:0/256.0 blue:0/256.0 alpha:1.0];    
    cell.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    cell.textLabel.textAlignment = UITextAlignmentLeft;
    cell.imageView.image = nil;

    if (indexPath.section == 0 && indexPath.row == 1)
    {
        if([[NSUserDefaults standardUserDefaults]boolForKey:@"password"] == YES)
        {
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
    }

    switch(indexPath.section)
    {

        case 2:
        {
            cell.textLabel.textAlignment = UITextAlignmentCenter;
        }
        break;

        case 3:
        {
            if (indexPath.row == 0) 
                cell.imageView.image = [UIImage imageNamed:@"facebook"];
            else if (indexPath.row == 1)
                cell.imageView.image = [UIImage imageNamed:@"twitter"];
            else if (indexPath.row == 2)
                cell.imageView.image = [UIImage imageNamed:@"youtube"];
            else
                cell.imageView.image = [UIImage imageNamed:@"vk"];
        }
        break;
        default:
        break;
    }




    return cell;
}