2016-09-16 7 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を使う方法は? Thnaks

+0

は、なぜあなたは '' MGSwipeTableCellのためのXIBを作成しませんか? –

+0

私はプロジェクト全体でxibを使用していません。私はすべてをコーディングする。 @ Mr.UB –

+0

サイドノートで、なぜコード内のすべてをやっている場合に自動レイアウトを使用していないのですか? – GoodSp33d

答えて

4

よりもむしろviewWithTagを使用して、あなたはセルの再利用識別子にカスタムクラスを登録することができます。そして、あなたはそのサブクラスのプロパティを使用してラベルにアクセスすることができます。

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 
} 

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(サードパーティライブラリ)も使用する必要があります。だから私はこれを行う方法がわからない –

+1

気づいたら、私のカスタムクラス_is_' MGSwipeTableCell'のサブクラスです。私は左右のボタンを設定しています。 – Rob

+0

@HirenPrajapatiあなたはrobが言及したようにサブクラス化する必要があります。 – 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'のメンバ 'TEXT'(2行目)がありません –

+1

私は編集しました、私はテストできません –

関連する問題