【发布时间】: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