我正在创建一个应用程序,我想在 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()
}
此代码构建并运行,但是当我设置的计时器到期并且运行更新函数时,两个可用单元格都更改为相同的标签。我想要做的是使第一个单元格可以保持静态,而新添加的单元格可以从“items”数组接收新元素,而无需使用indexpath.row(因为我将要使用的引号来自项目数组将没有特定的顺序)。任何帮助将不胜感激。
最佳答案
尝试使用 reloadRowsAtIndexPaths:
代替 reloadData
并传入要重新加载的行。然而,这确实使用了indexPath
。我知道重新加载特定行而不是整个 tableView 的唯一方法是通过索引路径指定行。
关于ios - 重新加载 TableView 以创建动态标签更改 iOS Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36393917/