簡體   English   中英

在UI表格視圖中滾動時文本會重疊嗎?

[英]Text get overlapped while scrolling in UI table view?

我已經嘗試過此方法,並且正在使用標簽作為表單元格的子視圖,滾動時標簽中的文本與先前的單元格標簽內容重疊

var tableCell: CommentTableViewCell? = tableView.dequeueReusableCellWithIdentifier("CommentCell") as? CommentTableViewCell
        tableCell!.selectionStyle = UITableViewCellSelectionStyle.None;
   if (tableCell == nil) {
            println("table view is working")
            tableCell = CommentTableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier:"CommentCell")
}

}

這是我滾動時得到的結果

您必須為cellAtIndexPath中的每個單元格設置屬性。 當您擁有圖片時,這類事情通常會發生,但文本也可以重現。 假設您有自定義單元格,那么每次都必須檢查該特定行是否具有標簽,文本,圖像等。

為什么會發生這種行為? 因為您的每個單元格都被重用,並且如果重用的單元格確實包含“新”單元格,圖像或其他內容中沒有的文本,那么這些文本將保留並造成不一致的錯誤。

您如何解決?

收到單元格后,嘗試設置空文本。 如果您的單元格是自定義的,則將標簽設置為空字符串,並將默認單元格標題標簽設置為空字符串。 然后從數組中檢索您的注釋並進行分配。 如果您提供更多代碼,我將向您發布應更改的內容。(我需要cellAtIndexPath方法)。

這是您的代碼:

var tableCell: CommentTableViewCell? = tableView.dequeueReusableCellWithIdentifier("CommentCell") as? CommentTableViewCell
let label:UILabel = UILabel(frame: CGRectMake(60, 25, 255, 50)) 
label.attributedText = text1 label.numberOfLines = 0 
label.lineBreakMode = NSLineBreakMode.ByWordWrapping; 
let font = UIFont(name: "Helvetica", size: 13.0) 
label.font = font label.sizeToFit(); 
tableCell!.addSubview(label) 

就像我在評論中說的那樣,您的問題是為每個重復使用的單元格添加標簽,這會造成重疊。 要解決此問題,請嘗試:

var tableCell: CommentTableViewCell? = tableView.dequeueReusableCellWithIdentifier("CommentCell") as? CommentTableViewCell
if(!tableCell)
{
    let label:UILabel = UILabel(frame: CGRectMake(60, 25, 255, 50)) 
    label.attributedText = text1 label.numberOfLines = 0 
    label.lineBreakMode = NSLineBreakMode.ByWordWrapping; 
    let font = UIFont(name: "Helvetica", size: 13.0) 
    label.font = font label.sizeToFit(); 
    label.tag = 1; //This is tag added
    // add text
    tableCell!.addSubview(label) 
}
else
{
        let label : UILabel = (UILabel *)cell.viewWithTag(1) as UILabel?
        // add text
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM