ios - 快速将多个 View 动态添加到 tableViewCell 时 UIViews 重叠
<p><p>我想在表格单元格中添加一些 <code>n</code>View (假设每个 View 都是一行)。根据 <code>designerExpertiseList</code> 计数,我为每一行创建了一个 View ,并添加了 1 个 ImageView 和 1 个标签。</p>
<p>但是当我滚动时,单元格的数据不正确。如果我长按一个单元格,我可以看到一个不同的 View 与现在可见的 View 重叠。请检查随附的屏幕截图
<br/><br/> 第一次加载 View 时:<a href="http://i.stack.imgur.com/M8itL.png" rel="noreferrer noopener nofollow">http://i.stack.imgur.com/M8itL.png</a>
<br/>我向下滚动后,再次向上滚动并长按:<a href="http://i.stack.imgur.com/AuTG0.png" rel="noreferrer noopener nofollow">http://i.stack.imgur.com/AuTG0.png</a>
<br/><br/>当我向上滚动时,第一次正确的几个单元格的数据,即使是搞砸了。我什至尝试在第一次渲染时只添加一次这些<code>动态 View </code>。
<br/><br/>有全局声明:</p>
<pre><code>let rowHeight:CGFloat = 20.0
let imageWidth:CGFloat = 15.0
let imageHeight:CGFloat = 15.0
let labelX:CGFloat = 30.0
let labelHeight:CGFloat = 20.0
var firstRender: = ()
</code></pre>
<p><br/>tableView中的代码<code>cellForRowAtIndexPath</code>方法:</p>
<pre><code>self.designer = AppController.getElementFromDesignersList(indexPath.section)
cell.designerName.text = designer.getFirstName() + " " + designer.getLastName()
cell.location.text = designer.getAddress()
// Add more ROWS of expertise if exist! Getting only 1st expertise now, if it exists
let expertiseList = self.designer.getExpertiseList()
if self.firstRender {
var i:Int = 0
for e in expertiseList {
let v = UIView(frame: CGRectMake(0, CGFloat(i)*rowHeight, cell.frame.width, rowHeight))
v.backgroundColor = UIColor.yellowColor()
let im = UIImageView(frame: CGRectMake(0, CGFloat(i)*imageHeight, imageWidth, imageHeight ))
//print("expertise image path: ", e.getImagePath())
im.af_setImageWithURL(
NSURL(string: e.getImagePath())!,
placeholderImage: UIImage(named: "default_galary_demo2")!
)
im.translatesAutoresizingMaskIntoConstraints = false
v.addSubview(im)
// Adding constraints
NSLayoutConstraint(item: im, attribute: .CenterY, relatedBy: .Equal, toItem: v, attribute: .CenterY, multiplier: 1, constant: 0).active = true
NSLayoutConstraint(item: im, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: v, attribute: NSLayoutAttribute.LeadingMargin, multiplier: 1.0, constant: 0.0).active = true
im.widthAnchor.constraintEqualToAnchor(nil, constant: imageWidth).active = true
im.heightAnchor.constraintEqualToAnchor(nil, constant: imageHeight).active = true
// cell.frame.width - im.frame.width - 50
let label = UILabel(frame: CGRectMake(0, CGFloat(i)*labelHeight, UIScreen.mainScreen().bounds.width - imageWidth, labelHeight))
label.font = UIFont(name: "OpenSans", size: 12)
print("expertise dump:", dump(e.getExpertiseValuesList()))
//print("expertise str: ", e.getExpertiseValuesList().map({"\($0.getName())"}).joinWithSeparator(","))
label.text = e.getExpertiseValuesList().map({"\($0.getName())"}).joinWithSeparator(",")
//label.translatesAutoresizingMaskIntoConstraints = false
v.addSubview(label)
NSLayoutConstraint(item: label, attribute: .CenterY, relatedBy: .Equal, toItem: v, attribute: .CenterY, multiplier: 1, constant: 0).active = true
NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: im, attribute: NSLayoutAttribute.LeadingMargin, multiplier: 1.0, constant: 10.0).active = true
cell.designerExpertiseView.addSubview(v)
i += 1
}
self.firstRender = false
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我认为仅在 <code>if self.firstRender</code> 时渲染单元格是不正确的,因为当用户滚动表格 View 时,屏幕外的单元格将被重用以显示其他不同索引路径。
另一件事,您创建约束,但不使用它们</p></p>
<p style="font-size: 20px;">关于ios - 快速将多个 View 动态添加到 tableViewCell 时 UIViews 重叠,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/37836510/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/37836510/
</a>
</p>
页:
[1]