(自定义)UITableViewCell滚动后混合

时间:2011-02-17 14:10:45

标签: iphone objective-c ios uitableview

我已经在这个问题上待了几个星期了。

基本上当我在TableView中向上/向下滚动时,thar使用在IB中设计的自定义单元格,所有内容都会混淆并放错位置

我尝试了多种解决方案,但无济于事,您将不得不原谅我的代码。

人们一直建议为表格单元格创建一个子视图,但我不知道如何做到这一点= /仍然是iOS开发的新功能所以如果您有可能的答案,请尽可能详细说明。< / p>

再一次,抱歉我的代码= /

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

    static NSString *MyIdentifier = @"MyIdentifier";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    NSInteger intCellTag;

    NSDictionary *dictionary = [[[self.tableDataSource objectAtIndex: indexPath.section] objectForKey: @"Rows"] objectAtIndex: indexPath.row];


    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];


        [[[NSBundle mainBundle] loadNibNamed:@"EventsCustomTVCell" owner:self options:nil] lastObject];
        cell = tvCell;
        self.tvCell = nil;


        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        cell.tag = intCellTag;


        intCellTag++;

        UIImage *customCellBG = [UIImage imageNamed:@"EventsCustomTableCellBG.png"];
        UIImageView *customCellBGImageView = [[UIImageView alloc] initWithImage: customCellBG];
        customCellBGImageView.contentMode = UIViewContentModeScaleToFill;
        cell.backgroundView = customCellBGImageView;
        [customCellBGImageView release];




        [cell.contentView addSubview:imgThumbnail];
        [cell.contentView addSubview:lblName];
        [cell.contentView addSubview:lblDescription];
        [cell.contentView addSubview:lblDate];

    }



    imgThumbnail.image = [UIImage imageNamed:[dictionary objectForKey: @"Thumbnail"]];
    lblName.text = [dictionary objectForKey:@"Title"];
    lblDescription.text = [dictionary objectForKey:@"Description"];
    lblDate.text = [dictionary objectForKey:@"Date"];


    return cell;


}

5 个答案:

答案 0 :(得分:1)

看起来你正试图在定义每个UITableViewCell时混合隐喻 - 从.xib加载,并手动创建子视图。这当然没有错,但你可以直接将图像和标签放入tableviewCell,如下所示:

enter image description here

以下是显示每一行的代码(当然,在IB中,您已经为每个要为每行定制的UIKit对象分配了非零唯一标记)

#define kImageTag 1
#define kLabel1Tag 2
#define kLabel2Tag 3
#define kLabel3Tag 4

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"MyTvCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) 
    {
        self.tvCell = nil;
        [[NSBundle mainBundle] loadNibNamed:@"TvCell" owner:self options:nil];
        cell = self.tvCell;
    }

    UIImageView *iv =  (UIImageView *) [cell viewWithTag:kImageTag];
    UILabel *lbl1 = (UILabel *) [cell viewWithTag:kLabel1Tag];
    UILabel *lbl2 = (UILabel *) [cell viewWithTag:kLabel2Tag];
    UILabel *lbl3 = (UILabel *) [cell viewWithTag:kLabel3Tag];

    iv.image = [UIImage imageNamed:@"myimage.png"];
    lbl1.text = @"howdy";
    lbl2.text = @"there";
    lbl3.text = @"foo";

    return cell;
}

答案 1 :(得分:1)

iOS Swift: I have done following to resolve my issue

let CellIdentifier: String = "CellIdentifier\(indexPath.section)\(indexPath.row)"

        var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier(CellIdentifier) as UITableViewCell?

        if cell == nil {
            cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: CellIdentifier)
        }

答案 2 :(得分:0)

您正在为每个单元格添加相同的子视图对象

[cell.contentView addSubview:imgThumbnail];
[cell.contentView addSubview:lblName];
[cell.contentView addSubview:lblDescription];
[cell.contentView addSubview:lblDate];

每个单元格都需要自己的一组子视图对象。

答案 3 :(得分:0)

imgThumbnail
lblName
lblDescription
lblDate

您应该为这些对象分配标签(您可以在界面构建器中执行此操作) 如果您配置单元格,则查询单元格以查找标记并设置其属性。

现在您保留对添加的最后一个单元格的标签的引用,每次表格要求新单元格时,您都会更改这些标签。

假设您将标记10分配给interfacebuilder中的标签lblDescription

然后你会替换

lblDescription.text = [dictionary objectForKey:@"Description"];

UILabel *lblDescription = (UILabel *)[cell viewWithTag:10];
lblDescription.text = [dictionary objectForKey:@"Description"];
编辑:我认为imgThumbnail等是您单元格的子视图,但是您再次添加它们。如果我的假设是正确的,你应该摆脱[cell.contentView addSubview ...]。

如果我错了你应该摆脱imgThumbnail等作为你的viewcontroller的实例变量。每次创建新单元格时添加单独的UIViews。就像你使用backgroundview一样。但是,在配置单元格值时,您已分配标记并使用该标记。

答案 4 :(得分:0)

您可以使用

NSString * cellIdentifier = [NSString stringWithFormat:@“%d”,indexPath.row];

而不是

static NSString * CellIdentifier = @“Cell”;