重新加载表视图以创建动态标签更改iOS Swift

时间:2016-04-04 02:57:51

标签: ios swift uitableview

我正在创建一个应用程序,我想在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(因为我要使用的引用items数组将没有特定的顺序)。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

而不是reloadData尝试使用reloadRowsAtIndexPaths:并传入要重新加载的行。但是,这确实使用了indexPath。我知道重新加载特定行而不是整个tableView的唯一方法是按索引路径指定行。

相关问题