2016-08-10 2 views
0

내 테이블보기가 충분히 아래로 스크롤되면 테이블보기에 있던 첫 번째 요소가 반복됩니다. 솔직히 왜 그런 일을하는지 모르겠다. 나의 유일한 추측은 그 셀이 이전에 존재했다면 어떻게 든 읽을 필요가 있지만 내가 본 튜토리얼에서는 그다지 문제가 없다는 것이다. cellForRowAtIndex 경로 메소드를 구현할 때 작동하며 셀을 반복하지 않습니다. 당신은 내가 들어오는 메시지는 단지 레이블을 포함하는 사용자 정의보기를 초기화 한 후 발신 또는 수신하고, 경우 확인하고 있습니다 뭐하는 거지를 모두 볼 수 있고, 나는 단지의 콘텐츠 뷰에 ​​추가로TableView 셀이 다시 표시되고 있습니다.

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let cellIdentifier = "chatbubbleCell" 
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! ChatBubbleCell 

    /* Configure the cell */ 
    var yAxis: CGFloat = 5.0 
    // Setup the chat bubble view 
    if outgoingSender { 
     let outgoingChatBubbleData = ChatBubbleData(text: messagesArray[indexPath.row], image: nil, profileImage: UIImage(named: "chatIcon"), date: NSDate(), type: .Mine) 
     let outgoingChatBubble = ChatBubble(data: outgoingChatBubbleData, startY: yAxis) 
     cell.contentView.addSubview(outgoingChatBubble) 
    } else { 
     let incomingChatBubbleData = ChatBubbleData(text: messagesArray[indexPath.row], image: nil, profileImage: UIImage(named: "chatIcon"), date: NSDate(), type: .Opponent) 
     var incomingChatBubble = ChatBubble(data: incomingChatBubbleData, startY: yAxis) 
     cell.contentView.addSubview(incomingChatBubble) 
    } 

    return cell 
} 

세포.

도움을 주시면 감사하겠습니다. 감사합니다.

- 아래의 Paul은 세포가 재사용되고 있다는 이유로 그 세포가 동일한 것을 보이고 있다고 말했습니다. cellForRowAtIndexPath에서

if outgoingSender { 
    cell.outgoingChatBubbleData.text = messagesArray[indexPath.row] 
    var incomingChatBubble = ChatBubble(data: cell.outgoingChatBubbleData, startY: yAxis) 
    // Attach to bubble view? 
} else { 
    cell.incomingChatBubbleData.text = messagesArray[indexPath.row] 
    let outgoingChatBubble = ChatBubble(data: cell.incomingChatBubbleData, startY: yAxis) 
    // Attach to bubble view? 
} 

그것의 속성을 설정 한 후

let outgoingChatBubbleData = ChatBubbleData(text: "", image: nil, profileImage: UIImage(named: "chatIcon"), date: NSDate(), type: .Mine) 
    let incomingChatBubbleData = ChatBubbleData(text: "", image: nil, profileImage: UIImage(named: "chatIcon"), date: NSDate(), type: .Opponent) 

을 표시하고 그래서 나는 내 프로토 타입 셀 클래스로 내 휴대 로직을 넣어하지만 지금 내 문제는 내가 "ChatBubble를 부착하는 방법을 잘 모르겠어요 것입니다 의 "cellForRowAtIndexPath 방법에

+2

셀 객체가 재사용됩니다. 'cellForRowAtIndexPath'에 하위 뷰를 추가하는 것은 나쁜 생각입니다. 왜냐하면 셀 객체가 재사용 될 때 이전에 추가 된 하위 뷰는 여전히 존재하기 때문입니다. 이전에 추가 한 하위 뷰를 삭제하거나 더 나은 방법으로 셀 프로토 타입에 채팅 풍선 뷰를 추가하고 'cellForRowAtIndexPath'에 데이터를 설정하여 매번 하위 뷰를 추가 할 필요가 없도록해야합니다. – Paulw11

+0

@ Paulw11 좋아, 나는 네가하는 말을 믿는다. 내가 먼저 가서 내 프로토 타입 셀 클래스에 내 논리를 넣어 - 만약 당신이 그것을 볼 수있는 방법을 제안 할 수있는 addSubView를 사용하지 않고보기를 첨부 할 수있는 좋은 방법을 제안합니다. 도와 줘서 고마워! – Fernando

+0

폴 (pool)이 제안하는 것은'UITableViewCell' 서브 클래스의 속성으로'ChatBubble'의 인스턴스를 추가하고 테이블 뷰 셀의 라이프 사이클에서 아마'awakeFromNib'에서 서브 뷰로 추가하거나 스토리 보드에 추가하는 것입니다 . 그런 다음'cellForRowAtIndexPath'에 데이터'ChatBubble'을 간단히 업데이트하십시오. – beyowulf

답변

0

하나 까다로운 방법 addSubview()를 사용하지 않고 셀 타입의 UIView의 어느 셀 INITING에 파단을 추가하는 것, 단지 cellForRowAtIndexPath에 파단 용 isHidden 속성을 설정. 원하는 것을 얻을 수 있습니다.

관련 문제