![](/img/trans.png)
[英]How to create dynamic collection view in a table view cell, without reloading table view swift?
[英]Reloading Table View to Create Dynamic Label Changes iOS Swift
我正在创建一个想要在UITableViewCell内的标签内显示不同字符串的应用程序。 但是,我不能使用indexPath.row,因为字符串的顺序不是特定的。
let one = "This is the first quote"
let two = "This is the second quote"
var numberOfRows = 1
var quoteNumber = 0
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let items = [one, two]
myTableView = tableView
let cell = tableView.dequeueReusableCellWithIdentifier("customcell", forIndexPath: indexPath) as! cellTableViewCell
let cellWidth = cell.frame.width
let cellHeight = cell.frame.height
cell.QuoteLabel.frame = CGRectMake(cellWidth/8, cellHeight/4, cellWidth/1.3, cellHeight/2)
let item = items[quoteNumber]
cell.QuoteLabel.text = item
cell.QuoteLabel.textColor = UIColor.blackColor()
if quoteNumber == 0 {
quoteNumber = 1
}
if numberOfRows == 1 {
numberOfRows = 2
}
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
var timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "update", userInfo: nil, repeats: true)
}
func update() {
dispatch_async(dispatch_get_main_queue()) {
self.myTableView.reloadData()
}
这段代码会生成并运行,但是当我设置的计时器到期并且运行了更新功能时,两个可用单元都变为相同的标签。 我正在尝试做的是使第一个单元格保持静态,而新添加的单元格可以在不使用indexpath.row的情况下从“项”数组接收新元素(因为我将要使用的引号来自items数组将没有特定的顺序)。 任何帮助,将不胜感激。
代替reloadData
尝试使用reloadRowsAtIndexPaths:
并传递要重新加载的行。 但是,这确实使用indexPath
。 我知道要重新加载特定行而不是整个tableView的唯一方法是按索引路径指定行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.