[英]Subview disappears on UITableViewCell Subclass on scrolling
我有一個在接口生成器中創建的UITableViewCell的子類。
我在加載時向該單元格添加了一個子視圖,盡管它是單元格底部的一個1.0f厚的灰色分隔符。
但是,每當我向下滾動並且帶有灰色條的單元格離開視圖,然后又向上滾動時,灰色條現在就消失了。 如果我使用重新加載表格的“刷新后刷新”方法刷新頁面,它將重新出現。
這是添加子視圖的代碼:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//Make a cell
ZSWTaskCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZSWTaskCellTableViewCell" forIndexPath:indexPath];
UIView *bottomSeperator = [[UIView alloc] initWithFrame:CGRectMake(60, cell.frame.size.height, cell.frame.size.width-60, 1)];
bottomSeperator.backgroundColor = [UIColor lightGrayColor];
[cell addSubview:bottomSeperator];
cell.taskLabel.font = [UIFont fontWithName:@"Avenir" size:18];
cell.categoryLabel.font = [UIFont fontWithName:@"Avenir-Medium" size:14];
cell.categoryLabel.textColor = [UIColor grayColor];
當您已經在使用子類時,您應該在單元格類本身中設置它的子視圖。
根據其他因素,您應該覆蓋initWithCoder:
和/或layoutSubviews
。 preparForReuse
也可能值得一提。
請注意,單元已被重用,並且您正在為每次重用添加操作員視圖。 根據行數和用戶滾動,將有很多視圖。
將分隔符視圖作為屬性添加到您的單元格子類中,並在單元格初始化期間僅創建一次。
您是否使用heightForRowAtIndexPath方法? 如果是,則增加其高度以解決我的問題,這使我發瘋。 我正在嘗試類似的修復
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.