对此似乎有很多问题,但是我无法使用其他人的答案,因此希望有人可以回顾一下我的操作方式。我正在尝试使用。我有两个自定义UITableViewCells,现在它们上面只有一个BOOL属性,多数民众赞成在样式上。

在根据返回的数据类型的cellForRowAtIndexPath方法中,我正在对单元格进行样式设置。如果数据是“月”标题,则其外观为细长的单元格,如果数据为“新闻”,则其外观为较大的白色单元格。



当表加载时,所有内容看起来都很不错,但是如果我向下滚动以创建更多单元,然后向上滚动,则将重新创建单元,最终滚动会变慢,因为我的内存不足。

当我设置断点时,dequeueReusableCellWithIdentifier总是返回nil,因此我的单元格从未被重用,这似乎是一个问题。

在这张图片中,您可以看到单元格彼此堆叠并弄乱了:

这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *NewsCellIdentifer = @"NewsCellIdentifier";
    static NSString *MonthCellIdentifier = @"MonthCellIdentifier";


    NSUInteger row = [indexPath row];
    NewsItem *item = [self.newsArray objectAtIndex:row];

    if (item.IsMonth == YES)
    {
        NewsMonthUITableViewCell *cell = [self.mytableView dequeueReusableCellWithIdentifier:MonthCellIdentifier];

        if (cell == nil)
        {
            cell = [[NewsMonthUITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MonthCellIdentifier];
        }

        // This handles any other "date" cells to allow for different spacing styles.
        if (item.IsMonth)
        {
            UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(0, 10, 400, 20)];
            av.backgroundColor = [UIColor clearColor];
            av.opaque = NO;
            av.image = [UIImage imageNamed:@"month-bar-bkgd.png"];
            UILabel *monthTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 150, 20)];;
            CGFloat font = 11.0f;
            monthTextLabel.font = [BVFont HelveticaNeue:&font];
            monthTextLabel.backgroundColor = [UIColor clearColor];
            monthTextLabel.font = [BVFont HelveticaNeue:&font];
            monthTextLabel.textColor = [BVFont WebGrey];
            monthTextLabel.text = item.Title;

            cell.backgroundColor = [UIColor clearColor];
            [cell.contentView addSubview:av];
            [cell.contentView addSubview:monthTextLabel];
        }

        return cell;

    }
    else
    {
        NewsUITableViewCell *cell = [self.mytableView dequeueReusableCellWithIdentifier:NewsCellIdentifer];

        if (cell == nil)
        {
            cell = [[NewsUITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NewsCellIdentifer];
        }

        cell.contentView.backgroundColor = [UIColor clearColor];

        UIView *whiteRoundedCornerView = [[UIView alloc] initWithFrame:CGRectMake(10,10,300,100)];
        whiteRoundedCornerView.backgroundColor = [UIColor whiteColor];
        whiteRoundedCornerView.layer.masksToBounds = NO;
        whiteRoundedCornerView.layer.cornerRadius = 3.0;
        whiteRoundedCornerView.layer.shadowOffset = CGSizeMake(-1, 1);
        whiteRoundedCornerView.layer.shadowOpacity = 0.5;

        [cell.contentView addSubview:whiteRoundedCornerView];
        [cell.contentView sendSubviewToBack:whiteRoundedCornerView];
        [cell.contentView addSubview:[self NewsItemThumbnailView:item]];
        [cell.contentView addSubview:[self NewsItemTextView:item]];
        [cell.contentView addSubview:[self NewsItemCornerIconIndicatorView:item]];

        return cell;

    }

    return nil;

}

感谢您的协助或建议!

最佳答案

在使用没有动态原型的情节提要板时,需要在if (cell==nil)块内放置创建和添加子视图的代码。否则,每次重新使用表格视图单元格时,都会再次添加所有子视图。

展望 future ,我的建议是将Storyboard与动态原型(带有子类UITableViewCells)一起使用,并在IB的单元格中进行自定义。

关于iphone - 自定义UITableViewCells不在UITableView上重用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15117698/

10-10 21:17