2016-09-16 4 views
2

저는 Swift를 처음 접했지만, 전에 Objective-C에서 일했습니다. 셀이 UITableView에서 재사용되고 있는지 여부를 확인하는 데 문제가 있습니다.Swift에서 ViewWithTag를 사용하는 방법은 무엇입니까?

let cell = tableView.dequeueReusableCellWithIdentifier(strCellId, forIndexPath:indexPath) as! MGSwipeTableCell 
    cell.backgroundColor = UIColor.clearColor() 

    let SMSObj = self.arraySMSContent[indexPath.row] as! SMSModel 

    let lblMessage = UILabel(frame: CGRectMake(15, 10, Constants.SCREEN_WIDTH/1.4, Constants.SCREEN_HEIGHT/11)) 
    lblMessage.text = SMSObj.strSMSContent 
    lblMessage.textAlignment = NSTextAlignment.Left 
    lblMessage.numberOfLines = 2 
    lblMessage.textColor = UIColor.whiteColor() 
    cell.contentView.addSubview(lblMessage) 

저는 MGSwipebleCell을 사용했습니다. lblMessage를 스크롤하는 동안 겹칩니다. 심지어 우리는 세포가 무관한지 여부를 확인할 수 없습니다. 이 상황에서 viewWithTag를 사용하는 방법은? Thyaks

+0

가 왜'MGSwipeTableCell'에 대한 XIB를 생성하지 않습니다

override func viewDidLoad() { super.viewDidLoad() tableView.registerClass(CustomCell.self, forCellReuseIdentifier: "CustomCell") } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as! CustomCell cell.smsLabel.text = ... return cell } 

? –

+0

전체 프로젝트에서 xib를 사용하지 않았습니다. 나는 코딩으로 모든 것을 해낸다. @ Mr.UB –

+0

참고로 모든 것을 코드로 수행하는 경우 자동 레이아웃을 사용하지 않는 이유는 무엇입니까? – GoodSp33d

답변

4

오히려 viewWithTag를 사용하는 것보다, 당신이 셀 재사용 식별자와 사용자 정의 클래스를 등록 할 수 있습니다. 그럼 당신은 그 서브 클래스의 속성을 사용하여 라벨에 액세스 할 수 있습니다

class CustomCell: MGSwipeTableCell { 
    var smsLabel: UILabel = { 
     let label = UILabel() 
     label.translatesAutoresizingMaskIntoConstraints = false 
     label.textAlignment = .Left 
     label.numberOfLines = 2 
     label.textColor = .whiteColor() 

     return label 
    }() 

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) { 
     super.init(style: style, reuseIdentifier: reuseIdentifier) 

     backgroundColor = .blueColor() 

     contentView.addSubview(smsLabel) 
     NSLayoutConstraint.activateConstraints([ 
      smsLabel.topAnchor.constraintEqualToAnchor(contentView.topAnchor, constant: 5), 
      smsLabel.bottomAnchor.constraintEqualToAnchor(contentView.bottomAnchor, constant: -5), 
      smsLabel.leadingAnchor.constraintEqualToAnchor(contentView.leadingAnchor, constant: 5), 
      smsLabel.trailingAnchor.constraintEqualToAnchor(contentView.trailingAnchor, constant: -5) 
     ]) 

     leftButtons = [MGSwipeButton(title: "Red", backgroundColor: .redColor())] 
     leftSwipeSettings.transition = .Rotate3D; 

     rightButtons = [MGSwipeButton(title: "Green", backgroundColor: .greenColor())] 
     rightSwipeSettings.transition = .Rotate3D; 
    } 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 

     fatalError("not needed") 
    } 
} 
+0

답변 주셔서 감사합니다. Swift에서 커스텀 클래스를 생성하는 것을 알고 있지만 MGSwipeTableCell (third party library)을 사용해야합니다. 그래서 어떻게 해야할지 모르겠다. –

+1

알아두면, 내 커스텀 클래스는 MGSwipeTableCell의 서브 클래스이다. 나는 심지어 왼쪽과 오른쪽 버튼을 설정합니다. – Rob

+0

@HirenPrajapati 당신은 랍에게 언급 한 것처럼 서브 클래스에 넣어야합니다. – GoodSp33d

1

문제를 해결하는 방법은 여러 가지가 있습니다. 시도하십시오 :

if let lblMessage = cell.contentView.viewWithTag(9999) as? UILabel { //Tag 
    lblMessage.text = SMSObj.strSMSContent 
}else{ 
    let lblMessage = UILabel(frame: CGRectMake(15, 10, Constants.SCREEN_WIDTH/1.4, Constants.SCREEN_HEIGHT/11)) 
    lblMessage.text = SMSObj.strSMSContent 
    lblMessage.textAlignment = NSTextAlignment.Left 
    lblMessage.numberOfLines = 2 
    lblMessage.textColor = UIColor.whiteColor() 
    lblMessage.tag = 9999 //Tag 
    cell.contentView.addSubview(lblMessage) 
} 
+0

감사합니다. 그게 내가 필요한 것. 하지만 컴파일 오류가 발생합니다 : 형식의 값 'UIView'멤버가 없습니다 '두 번째 줄에'() –

+1

내가 편집 할 수 없습니다, 나는 테스트를 수행 할 수 없습니다. –

관련 문제