UITableViewCell内にあるラベル内に異なる文字列を表示するアプリケーションを作成しています。しかし、私はindexPath.rowを使用することはできません。なぜなら、文字列は特定の順序ではないからです。ダイナミックラベルの変更を作成するテーブルビューをリロードするiOS Swift
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()
}
このコードがビルドされ、実行されますが、私が設定したタイマーが満了し、更新機能が実行されたときに、利用可能な2つのセルの両方が同じラベルに変更します。私がしようとしているのは、最初に追加したセルがindexpath.rowを使わずに 'items'配列から新しい要素を受け取ることができるように、最初のセルを静的にできるようにすることです(引用符はitems配列の順序は特にありません)。どんな助けもありがとう。
データは整理されている必要があります。どのように他の行を適切に表示できますか?データはindexPathによってインデックス可能でなければなりません。その事実を回避することはありません。 – rmaddy