2014-11-19 3 views
0

내 tableview 셀에 삽입 할 UILabels을 만드는 데 문제가 있습니다. 그들은 벌금을 삽입하고 잘 표시합니다. 문제는 제가 스크롤 할 때 나타납니다. 그 행렬의 순서는 엉뚱한 방향으로 간다. 예를 들어, 처음에는 idLabel의 셀 텍스트가 0> 14에서 순차적으로 이동합니다. 아래로 스크롤 한 후 5, 0, 10, 3 등의 순서로 배열 할 수 있습니다.각 tebleview 셀에 UILabels를 올바르게 삽입하는 방법

왜 이런 일이 발생했는지에 대한 아이디어가 있습니까? 셀 생성이 잘못 된 후 레이블을 업데이트하려는 시도를하고 있습니다.

var idArr: NSMutableArray! 

// In init function 
self.idArr = NSMutableArray() 
for ind in 0...14 { 
    self.idArr[ind] = ind 
} 

// Create the tableview cells 
internal func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    if cell == nil { 
     let id = self.idArr[indexPath.row] as Int 
     cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell") 

     // Id 
     let idLabel: UILabel = UILabel(frame: CGRectZero) 
     idLabel.text = String(id) 
     idLabel.textColor = UIColor.blackColor() 
     idLabel.tag = indexPath.row 
     idLabel.sizeToFit() 
     idLabel.layer.anchorPoint = CGPointMake(0, 0) 
     idLabel.layer.position.x = 15 
     idLabel.layer.position.y = 10 
     idLabel.textAlignment = NSTextAlignment.Center 
     cell?.addSubview(idLabel) 
    } 

    // Update any labels text string 
    for obj: AnyObject in cell!.subviews { 
     if var view = obj as? UILabel { 
      if view.isKindOfClass(UILabel) { 
       if view.tag == indexPath.row { 
        view.text = String(id) 
       } 
      } 
     } 
    } 

    // Return the cell 
    return cell! 
} 

어떤 도움을 주셔서 감사합니다. 더 이상 나무를 볼 수 없습니다.

답변

1

셀 업데이트의 논리가 눈에 띄지 않습니다. 초기화 할 때 설정 한 태그가 색인 경로의 행과 동일하지만 셀을 재사용해야하기 때문에 항상 그렇지는 않습니다. for 루프에서 해당 태그 검사 행을 제거하면 제대로 작동합니다.

또한 선택적으로 어쨌든 뷰로 뷰를 캐스팅 한 후에 왜 ifKindOfClass를 검사하는지 확신 할 수 없습니다.

실수로 Apple의 뷰를 업데이트하지 않으려면 레이블에 상수 태그를 추가하고 모든 하위 뷰를 반복하지 말고 해당 태그로 레이블을 가져 오는 것이 좋습니다.

id 변수가 적절한 범위에없고 셀의 정의가 없기 때문에 이것은 실제 코드가 아닙니다. 적절한 dequeueing을 추가하고 id 변수를 작동하는 것으로 이동시킵니다. 나는 당신이 당신의 실제적인 코드에서 그것들을하고 있다고 가정하고있다.

internal func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let labelTag = 2500 
    let id = self.idArr[indexPath.row] as Int 

    var cell = tableView.dequeueReusableCellWithIdentifier("cell") 

    if cell == nil { 
     cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "cell") 

     // Id 
     let idLabel: UILabel = UILabel(frame: CGRectZero) 
     idLabel.text = String(id) 
     idLabel.textColor = UIColor.blackColor() 
     idLabel.tag = labelTag 
     idLabel.sizeToFit() 
     idLabel.layer.anchorPoint = CGPointMake(0, 0) 
     idLabel.layer.position.x = 15 
     idLabel.layer.position.y = 10 
     idLabel.textAlignment = NSTextAlignment.Center 
     cell?.addSubview(idLabel) 
    } 

    // Update any labels text string 
    if let view = cell?.viewWithTag(labelTag) as? UILabel { 
     view.text = String(id) 
    } 

    // Return the cell 
    return cell! 
} 
+0

태그 검사를 제거하면 모든 셀의 모든 레이블을 현재 셀 인덱스의 값으로 설정합니다. 다시 사용 되더라도이 경우 행에 반드시 0> 14 인덱스가 유지됩니까? 그래서 논리가 작동 할 것이라고 생각했습니다. – PersuitOfPerfection

+0

뷰에 대한 참조없이 하위 뷰를 반복하면 그 모든 셀의 레이블이 ID 문자열로 설정됩니다. 다른 셀은 해당 셀의 하위 뷰를 통해서만 영향을 받으므로 영향을받지 않습니다. 따라서 하위 뷰를 반복하지 않고 뷰를 검색하기 위해 태그를 참조로 사용해야합니다. 코드 업데이트 – bjtitus

+0

좋은 아이디어, 훨씬 좋네요.이게 내 문제를 해결해 줬어. 각 라벨에 개별 태그를 설정하는 것에 대한 혼란은 (더 멋진 업데이트 제안과 함께)이를 버리고있는 것이 었습니다. 정말 고마워! – PersuitOfPerfection

관련 문제