2016-03-12 53 views
0

我有問題將頂部邊框設置爲UITableViewCell。我的要求是爲所有單元格設置底部邊框,並僅爲第一個單元格設置頂部邊框。我用下面的代碼來設置此問題在UITableViewCell中設置邊框並在滾動時閃爍

if (indexPath.row == 0){ 
    CALayer* topCellBorder = [CALayer layer]; 
    topCellBorder.frame = CGRectMake(0, 1, CGRectGetWidth(cell.frame) , 0.5); 
    topCellBorder.backgroundColor = [config getColour:@"theme.table-border.color"].CGColor; 
    [cell.layer addSublayer:topCellBorder]; 
} 
CALayer* bottomCellBorder = [CALayer layer]; 
bottomCellBorder.frame = CGRectMake(0, CGRectGetHeight(cell.frame) - 0.5, CGRectGetWidth(cell.frame) , 0.5); 
bottomCellBorder.backgroundColor = [config getColour:@"theme.table-border.color"].CGColor; 
[cell.layer addSublayer:bottomCellBorder]; 

這工作得很好,在第一,繼承人的截圖 enter image description here

但滾動後,頂部邊框消失了,加入到這樣

下一個視圖

enter image description here

此外,我看到邊界閃爍時滾動。任何想法是什麼造成這種行爲

回答

1

細胞正在重新使用,所以舊的邊界仍然存在。當您滾動第一個單元格時,它將被重新用於某個其他單元格,並且它已經具有topCellBorder

我建議你在單元格的awakeFromNib方法中添加單元格邊框,並簡單地打開和關閉頂部和底部邊框。因此,您的單元應具有兩個邊框圖層,但基於表格視圖數據源中的indexPath.row,您只需顯示並隱藏它們即可。

+0

謝謝@Stefan。是的,我認爲細胞再利用是問題。但'awakeFromNib'是個好主意。將嘗試看到 – RameshVel

+0

perfecto,它的工作 – RameshVel