【问题标题】:UITableViewCell UIView Alignment issueUITableViewCell UIView 对齐问题
【发布时间】:2017-06-07 20:31:35
【问题描述】:

我设计自定义表格视图单元格。当我滚动 TableView 时,单元格视图移动第一个和最后一个单元格的 x 位置。知道为什么会出现这个问题吗?

我放了一些代码和图片供你理解问题。

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CreateQuestionViewCell
        //let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CreateQuestionViewCell

        //let subview = cell.viewWithTag(1)!

        //let textDetail = cell.viewWithTag(2) as! UILabel
        cell.quesLbl.text = ((arrTableData[indexPath.row] as! Question).que).convertHTMLtoSymbols()

        //let edit = cell.viewWithTag(3) as? UIButton
        cell.editBtn.tag = indexPath.row
        cell.editBtn.addTarget(self, action: #selector(EditButActions(sender:)), for: .touchUpInside)

        //let deleteBut = cell.viewWithTag(4) as? UIButton
        cell.deleteBtn.tag = indexPath.row
        cell.deleteBtn.addTarget(self, action: #selector(deleteButActions(sender:)), for: .touchUpInside)

        if NotEditable {
            cell.editBtn.isHidden = true
            cell.deleteBtn.isHidden = true
        } else {
            cell.editBtn.isHidden = false
            cell.deleteBtn.isHidden = false
        }
        cell.quesLbl.font = UIFont(name: "Roboto-Regular", size: 14)!
        let desiredWidth: CGFloat = UIScreen.main.bounds.size.width - 60
        let size: CGSize = cell.quesLbl.sizeThatFits(CGSize.init(width: desiredWidth, height: CGFloat.greatestFiniteMagnitude))
        cell.quesLbl.numberOfLines = 0
        cell.quesLbl.lineBreakMode = NSLineBreakMode.byWordWrapping
        if NotEditable {
            cell.subview.frame = CGRect.init(x: 10, y: 10, width: Int(UIScreen.main.bounds.width - 40), height: Int(20 + size.height))
        } else {
            cell.subview.frame = CGRect.init(x: 10, y: 10, width: Int(UIScreen.main.bounds.width - 40), height: Int(50 + size.height))
        }
        cell.subview.layer.cornerRadius = 2.0
        cell.subview.layer.borderWidth = 1.0
        cell.subview.layer.borderColor = UIColor.clear.cgColor
        // SubView.layer.masksToBounds = true;

        cell.subview.layer.shadowColor = UIColor.init(red: 200 / 255, green: 200 / 255, blue: 200 / 255, alpha: 1).cgColor
        cell.subview.layer.shadowOffset = CGSize.init(width: 0.5, height: 0.5)
        cell.subview.layer.shadowRadius = 2.0
        cell.subview.layer.shadowOpacity = 1.0
        cell.subview.layer.masksToBounds = false
        //cell.subview.layer.shadowPath = UIBezierPath(roundedRect: cell.subview.bounds, cornerRadius: cell.subview.layer.cornerRadius).cgPath
        cell.layoutIfNeeded()
        return cell
    }

【问题讨论】:

  • 你从哪里重新加载你的tableView?你是从主线程做的吗?
  • @BadhanGanesh 感谢您的回复。是的,我在主线程中重新加载,但是当我不断上下滚动而不是顶视图时,在单元格中移动 x 位置。查看图像。 :)

标签: ios objective-c uitableview uiview swift3


【解决方案1】:

因此,请记住这些单元格是重复使用的,无论何时您向cellForRowAt: 中的单元格添加任何内容,您都将其添加到表格显示的每个后续单元格中。

在情节提要单元原型中设置您所能做的一切。添加按钮和子视图,设置目标,并使用情节提要界面构建器添加任何约束。

然后在cellForRowAt: 中,您只需要设置在单元格之间发生变化的任何内容、标签中的文本字符串或图像视图中的图像...如果您在单元格中使用按钮,请继续设置按钮标签如cell.editBtn.tag = indexPath.row,以便在按钮目标函数中识别单元格。但是,避免在目标函数中直接更改单元格,而是根据标签从数据源中获取数据,并使用reloadData重绘表格。

希望这会有所帮助。

【讨论】:

  • 感谢您的回复。我在单元格中设置了所有视图并访问表单标记,但我的问题是当我滚动表格视图时,最顶部的单元格视图会像图像中那样移动一点 x 位置。是什么原因,我该如何解决。谢谢
  • 原因是因为你在操作cell.subview,干扰了表格的布局。取出所有 cell.subview 调用(尤其是帧更改)和“cell.layoutIfNeeded()”,你应该没问题。 UITableView 很好地布置了单元格。
猜你喜欢
  • 2012-08-25
  • 2011-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-16
  • 1970-01-01
  • 1970-01-01
  • 2013-01-07
相关资源
最近更新 更多