[英]How to reset tableViewCell's content in iOS?
我創建自定義單元格,並為某些單元格添加新的subView:
static NSString *cellIdentifierCell = @"Cell";
FeedCell *cell = (FeedCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifierCell];
if (!cell)
{
cell = [[FeedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifierCell]; //frame
}
//here add subView
if (wallPost.attachmentLinks[@"audio"])
{
NSLog(@"%ld",(long)indexPath.row);
[cell addAudio:wallPost.attachmentLinks[@"audio"] fromStartFrame:CGRectMake(10, 50 + collectionHeight + 5 + 90 + 5, 300, 20)];
} else {
for (UIView *v in cell.subviews) //// (1)
{
if (v.tag == 333)
{
[v removeFromSuperview];
}
}
}
這是我使用的單元格方法:
- (void)addAudio:(NSArray *)arrayAudio fromStartFrame:(CGRect)frame
{
for (NSDictionary *dic in arrayAudio)
{
UIView *audioView = [[UIView alloc]init];
audioView.frame = CGRectMake(10, frame.origin.y-100, 300, 20);
audioView.backgroundColor = [UIColor yellowColor];
audioView.tag = 333;
[self addSubview:audioView];
}
[self layoutSubviews];
}
問題是我在子視圖中必須沒有子視圖。 第(1)部分未運行。 如何重置單元格或從中刪除自定義視圖?
創建兩個單元格視圖會更好。 這不是使用標簽刪除視圖的最佳方法。 在代碼的其他部分,只需使用備用視圖即可將其顯示為空白視圖。 它可能是處理這種情況的替代方法。
在添加新視圖之前,應始終刪除舊視圖。 所以代碼應該像這樣
for (UIView *v in cell.subviews) //// (1)
{
if (v.tag == 333)
[v removeFromSuperview];
}
if (wallPost.attachmentLinks[@"audio"])
{
NSLog(@"%ld",(long)indexPath.row);
[cell addAudio:wallPost.attachmentLinks[@"audio"] fromStartFrame:CGRectMake(10, 50 + collectionHeight + 5 + 90 + 5, 300, 20)];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.