似乎有很多关于此问题的问题,但我无法使用任何人的答案,所以希望有人可以回顾一下我是如何做到这一点的。我正在尝试使用。我有两个自定义的UITableViewCells,它们现在对它们只有一个BOOL属性,这就是它的样式。自定义UITableViewCells没有在UITableView上重复使用
在我的cellForRowAtIndexPath
方法基于什么类型的数据回来我正在设计我的细胞。如果数据是“月”标题,那么它就是一个看起来很长的细胞,如果它是“新闻”,它将成为一个更大的白色细胞。
当表加载一切看起来很大,但如果我向下滚动,以创造更多的细胞,达到细胞被重建,并最终滚动减慢,因为我跑出来的内存中,然后向后滚动。
当我设置中断点时dequeueReusableCellWithIdentifier
总是返回零,所以我的细胞不会重用,这似乎是一个问题。
在这张图片中可以看到,细胞越来越堆叠在彼此的顶部和搞砸:
这里我我的代码:
- (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;
}
感谢您的任何援助或咨询!
你在使用故事板吗? – 2013-02-27 16:55:11
您是否正在使用动态原型的故事板?如果是,请检查属性检查器中的每个单元格“标识符”是否设置正确。另外,如果你不使用故事板和动态原型,你创建和添加子视图的代码应该放在你的if(cell == nil)块中。我的建议是使用动态原型的Storyboard并在IB中的单元格中进行自定义。 – 2013-02-27 16:57:13
我正在使用Storyboard,但我没有使用Prototyped单元格或在故事板中设计我的单元格。我只是将我的TableView放在屏幕上,但以编程方式完成剩下的工作。 – Flea 2013-02-27 16:57:36