1

我有一个 UITableView ,其方法cellForRowAtIndexPath如下所示,问题是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;
}
4

4 回答 4

1
[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 方法中),然后将其添加到单元格的内容视图中,如下所示

[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

于 2012-08-09T13:14:49.037 回答
0

您面临的问题是由于单元格的重用标识符。此外,每次执行 cellForRowAtIndexPath 时,您都在创建 Imageview。

我会在这里为你推荐两个解决方案。

第一个选项:-只需替换此代码:

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

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

第二个选项:-只需使用此格式化代码,这不会每次只使用当前单元格中的对象和标签来创建图像视图。

    - (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"];

}
于 2012-08-09T12:36:49.873 回答
0

正如许多评论中所捕获的那样,存在许多问题:

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

使用 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];

于 2012-08-09T12:38:08.143 回答
0

使用以下代码修复:

UIImageView *cellImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"next@2x.png"]];
[cell setAccessoryView:cellImage];
于 2012-08-14T14:51:09.230 回答