swift - 如何在 Swift 中使用 ViewWithTag?

标签 swift uitableview viewwithtag

我是 Swift 新手,但我以前使用过 Objective-C。我在 checkin 单元格是否在 UITableView 中重用时遇到问题。

    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 重叠。即使我们也无法检查 cell 是否为零。那么这种情况下如何使用viewWithTag呢?谢谢

最佳答案

您可以使用单元重用标识符注册自定义类,而不是使用 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")
    }
}

关于swift - 如何在 Swift 中使用 ViewWithTag?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39526267/

相关文章:

swift - UITableView 不会滚动到模糊 View 、选项卡栏或导航栏后面

ios - 每个 TableViewCell 的 IBAction 不返回唯一的 NSIndexPath

ios - 每次我滚动 cell.viewWithTag 都会在展开发现 nil 时给出 fatal error

SWIFT:谷歌地图绘制航路点折线

ios - 在 ScrollView -Swift 项目中随时触摸隐藏键盘

ios - 如何在行的滑动操作配置中为 VoiceOver 添加辅助功能标签?

ios - 如果从另一个类调用 removeFromSuperview 则不起作用

swift - GetStream EnrichedActivity 类的复制问题

objective-c - 我可以自定义现有的 viewWithTag :(NSInteger) method for stringTag?

swift - 无法找到带有标签的 View