繁体   English   中英

单元格图像仅在滚动后出现

[英]Cell image only appearing after scrolling

我有一个带有方法cellForRowAtIndexPath的UITableView,如下所示,问题是cellImage仅在完全向上滚动(因此看不到任何行)然后释放后才出现。 奇怪的是,对于另一个单独的表视图,我在另一个类中具有相同的代码,并且图像看起来不错。 有任何想法吗?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    [cell.textLabel setText:[items objectAtIndex:indexPath.row]];
    [cell.textLabel setFont:[UIFont fontWithName:@"Helvetica" size:15.0f]];
    [cell.textLabel setTextColor:[UIColor whiteColor]];
    [cell setBackgroundColor:[[UIColor alloc] initWithRed:117.0/255.0 green:118.0/255.0 blue:121.0/255.0 alpha:1.0]];
    [self.tableView setBackgroundView:nil];
    [self.tableView setBackgroundView:[[UIView alloc] init]];
    [self.view setBackgroundColor:[[UIColor alloc] initWithRed:167.0/255.0 green:169.0/255.0 blue:172.0/255.0 alpha:1.0]];
    UIImageView *cellImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"next@2x.png"]];
    [cell addSubview:cellImage];
    [cellImage setFrame:CGRectMake(0, 0, 26, 24)];
    [cellImage setCenter:CGPointMake(cell.frame.size.width - 30, cell.frame.size.height/2)];

    return cell;
}
[self.tableView setBackgroundView:nil];
[self.tableView setBackgroundView:[[UIView alloc] init]];

这些行不属于该方法。 每次滚动每个单元格时都调用方法。 创建表视图之后,将它们放入viewDidLoad方法中。

UIImageView *cellImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"next@2x.png"]];
[cell addSubview:cellImage];

这些行确实非常糟糕,并可能导致内存泄漏。

每次滚动表格视图时,都会向该单元格添加一个图像视图。 表格视图重用了单元格。 这意味着当单元被重用时,您可以向该单元添加一个新的图像视图。 滚动更多次后,除非在代码中的某个位置将其删除,否则每个单元格上都有x倍的imageview。

另一点是,如果您想向某个单元格添加一些子视图(例如,在该单元格的init方法中),则可以像这样将其添加到该单元格的contenview中

[cell.contentView addSubview:someView];

否则,您的子视图可能与附件视图重叠。 调整单元格大小时,您可能会遇到问题。

我建议您从以下链接阅读该指南: http : //developer.apple.com/library/ios/#documentation/userexperience/conceptual/tableview_iphone/TableViewCells/TableViewCells.html#//apple_ref/doc/uid/TP40007451- CH7-SW1

您面临的问题归因于cell的reuseIdentifier。 另外,每次执行cellForRowAtIndexPath时,您都将创建Imageview。

我会在这里为您提出两种解决方案。

第一种选择:-只需替换此代码:

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    cell.accessoryType = UITableViewCellAccessoryNone;
}

在处理动态图像时,这不是正确的方法,这意味着您正在从服务器下载缩略图,并且数据内容更多,如果它较小且为静态数据,则对您来说很好。

第二个选择:仅使用此格式化代码,每次使用当前单元格中带有标签的对象时,都不会创建imageview。

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


    static NSString *CellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
        UIImageView *cellImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 26, 24)];  
        cellImage.tag = 88;
        [cell addSubview:cellImage];
        [cellImage setCenter:CGPointMake(cell.frame.size.width - 30, cell.frame.size.height/2)];

    }
    [cell.textLabel setText:[items objectAtIndex:indexPath.row]];
    [cell.textLabel setFont:[UIFont fontWithName:@"Helvetica" size:15.0f]];
    [cell.textLabel setTextColor:[UIColor whiteColor]];
    [cell setBackgroundColor:[[UIColor alloc] initWithRed:117.0/255.0 green:118.0/255.0 blue:121.0/255.0 alpha:1.0]];
    [self.tableView setBackgroundView:nil];
    [self.tableView setBackgroundView:[[UIView alloc] init]];
    [self.view setBackgroundColor:[[UIColor alloc] initWithRed:167.0/255.0 green:169.0/255.0 blue:172.0/255.0 alpha:1.0]];
    UIImageView *cellImage = (UIImageView *)[cell viewWithtag:88];
    cellImage.image = [UIImage imageNamed:@"next@2x.png"];

}

正如许多评论所记录的那样,存在许多问题:

在viewDidLoad方法(或通过XIB)而不是cellForRowAtIndexPath中设置视图和tableView背景

使用cell.imageView属性设置左图标,或者使用[cell.contentView addSubview:cellImage]构建自定义单元。 如果要执行后者,则还应该以与UILabel相同的方式添加textLabel。

要设置表格视图单元格的背景,请使用以下委托方法:

-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell*)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
 [cell setBackgroundColor:[[UIColor alloc] initWithRed:117.0/255.0 green:118.0/255.0 blue:121.0/255.0 alpha:1.0]]; 
}

虽然为了提高效率,您应该在视图控制器中实例化一次此UIColor并将其设置为[cell setBackgroundColor:self.cellBackground];

使用以下代码修复:

UIImageView *cellImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"next@2x.png"]];
[cell setAccessoryView:cellImage];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM