【问题标题】:UIView inside UITableViewCell position issue - UPDATEDUITableViewCell 位置问题内的 UIView - 已更新
【发布时间】:2015-11-30 19:57:22
【问题描述】:

这可要了我的命.. (.mov from my issue)

我想将我的自定义视图放置在我的自定义 tableViewCell 中。 做了这个简单的定位表达式: If : indexpath.row % 2 == 0 ---> 设置左侧视图(如 x: 10, y: 10) else:向右。

当应用启动时,所有气泡都在彼此下方。当我向下滚动到末尾时,它会重新定位。不是全部,而是其中一些。

我开始了我的代码,但还没有完成,因为这个小东西。我的代码如下:

TableViewController

class QuestionsTableViewController: UITableViewController {

    let c = "cell"
    var users = [User]()

    override func viewDidLoad() {
        super.viewDidLoad()
        for i in 0..<6 {
        let user = User(firstName: "\(i) Karl", lastName: "May")
            users.append(user)
        }
    }
    // MARK: - Table view data source

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return users.count ?? 0
    }


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

        let cell = tableView.dequeueReusableCellWithIdentifier(c, forIndexPath: indexPath) as! QuestionTableViewCell

          if indexPath.row % 2 == 0 {
        cell.bubleView.frame.origin =  CGPoint(x: origin.x + 10, y: origin.y + 10)
        cell.bubleView.backgroundColor = UIColor.blueColor()
    } else {
        cell.bubleView.frame.origin =  CGPoint(x: origin.x + 50, y: origin.y + 10)
        cell.bubleView.backgroundColor = UIColor.greenColor()
    }
        // Configure the cell...
        cell.nameLabel.text = users[indexPath.row].firstName + " " + users[indexPath.row].lastName

        return cell
    }
}

TableViewCell

class QuestionTableViewCell: UITableViewCell {

    @IBOutlet weak var bubleView: UIView!
    @IBOutlet weak var nameLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    func positionBuble() {
        bubleView.frame.origin = CGPoint(x: contentView.frame.origin.y+10, y: contentView.frame.origin.y+10)
    }
}

据我所知,这段代码应该可以工作,对吧?我在 Xcode Utilites 窗格上遗漏了什么吗? 我使用 Xcode 7.1.1

【问题讨论】:

  • 可能有帮助的一件事是,从外观上看,您永远不会“取消调用”气泡框以在 UITableViewCell 中重置,并且由于您使用的是重用单元格,因此您必须将单元格重置为您认为的任何状态,“正常状态”,以便单元格重新对齐到您想要的位置,否则单元格将保留占用其内存槽的前一个单元格的值
  • 您可以在自定义 UITableViewCell swift 文件中调用 "-(void)prepareForReuse {}您的规范化代码到覆盖的“prepareForReuse”方法中,然后每次 tableView 将数据从数据源传递到视图中的单元格时重置单元格。
  • 这只会部分解决您的头痛问题。单元格的另一部分从一开始就显示为被固定在左侧或右侧,我不得不弄乱你的代码,看看我们可以从中强制执行什么,而我没有时间这样做,所以看看准备细胞以供重复使用,看看你会发现什么。这应该可以解决当它应该被钉在右边或左边时,单元格被卡在左边或右边的问题。您在 cellforRowAtIndexPath 中的 if/then 语句没有“else”,您可以在其中调用以取消设置 TableViewCell 的视图,.. 所以
  • ... 从上面继续... 所以您可以在 PrepareForReuse 中重置视图,也可以在“cellForRowAtIndexPath”中完成“else”语句,无论哪种方式,您都将获胜。 Else 语句应根据 if 子句中的模 2 检查器将视图重置为左侧或右侧来对抗 if 语句。使用“else”子句将允许您不必使用 PrepareForReuse,因为视图的设置将发生在 Else 语句中,并为通过 cellforRowAtIndexPath 的每个单元格重置。这是您从重用中看到的副作用。
  • 谢谢,但我是这样开始的:“我开始了我的代码,但还没有完成”。我尝试了首先,然后在这里发布我的问题。没有结果

标签: ios swift2 xcode7.1


【解决方案1】:

问题是您缺少else

    if indexPath.row % 2 == 0 {
        let origin = cell.bounds.origin
        //cell.positionBuble()
        cell.bubleView.frame.origin =  CGPoint(x: origin.x + 10, y: origin.y + 10)
        cell.bubleView.backgroundColor = UIColor.blueColor()
    } else {
        // what????
    }

您需要这个,因为单元格可以重复使用。 indexPath.row % 2 == 0 的单元格将再次使用 indexPath.row % 2 != 0,但您没有进行任何更改,因此它们最终看起来都一样。

【讨论】:

  • 是的,按照 Matt 在这里展示的方式进行操作,这就是我要说的大部分内容,这可能会让您一直获得您的 tableview 所需的答案
  • 非常感谢您的代码,但我的初始代码是(没有效果,所以删除它): if indexPath.row % 2 == 0 { cell.bubleView.frame.origin = CGPoint(x: origin .x + 10, y: origin.y + 10) cell.bubleView.backgroundColor = UIColor.blueColor() } else { cell.bubleView.frame.origin = CGPoint(x: origin.x + 50, y: origin.y + 10) cell.bubleView.backgroundColor = UIColor.greenColor() } 链接:dropbox.com/sh/cyqjqafw2jxz7bw/…
【解决方案2】:

所以,经过多次尝试,我创建了另一个原型单元,在其中设置了设计元素和运行时属性,然后称为新单元的标识符。虽然,仍然不知道为什么我的其他方法没有奏效......

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> QuestionTableViewCell {
        var cella: QuestionTableViewCell!

        if indexPath.row % 2 == 0 { //c is an identifier String
            let cell = tableView.dequeueReusableCellWithIdentifier(c, forIndexPath: indexPath) as! QuestionTableViewCell
            cella = cell
        } else { //b is also
            let cell = tableView.dequeueReusableCellWithIdentifier(b, forIndexPath: indexPath) as! QuestionTableViewCell
            cella = cell
        }
        // Configure the cell...
        cella.nameLabel.text = users[indexPath.row].firstName + " " + users[indexPath.row].lastName
        //cella.tralala.blabla = ...other properties
        return cella
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多