2016-05-13 68 views
0

我有一個表格視圖,並在單元格中放置了指示喜歡count的文本標籤。例如,當我加載應用程序時,喜歡count顯示3.但是,當我向下滾動然後返回到頂部時,這3個將成爲不同的數字。如何防止文本標籤重複使用單元格?

我知道這是因爲細胞重複使用。如何爲每個單元格保存這些數字,而不是在滾動時更新它們?

圖像I \ VE緩存與AlamofireImage,但我不知道該怎麼做這些文本標籤

+0

請發表相關的代碼 – Shubhank

+0

您只需在'cellForRowAtIndexPath'中設置標籤的值 - 單元格只是數據的一個視圖,所以重用不應該是一個問題,但是,請將您的'cellForRowAtIndexPath'作爲最小值 – Paulw11

回答

0

你缺少的部分是你需要使用行索引來索引到你的數據。顯然,我們看不到你的代碼,但它聽起來像你沒有使用這個。通常,您有一組數據,並在cellForRowAtIndexPath中使用索引查找正確的數據元素。

0
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! { 

let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath:indexPath) as! MyCell 

cell.textLabel.text = "\(likesCount)" 

return cell 
} 
0

您的手機是UITableViewCell的子類嗎?

通過IB給你的一個標識符(再利用標識符),然後

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    MyTableViewCellSubclass *cell = [tableView dequeueReusableCellWithIdentifier:@"your_identifier"]; 
    // .. your code 
} 

而在子類

- (void)prepareForReuse 
{ 
    self.yourCountLabel.text = @"0"; // "reset" value 
} 
0

你不阻止它重新定義方法(除非你想絕對會殺死你的應用程序的性能)。你擁抱它。

在cellForRowAtIndexPath中,您會得到一個之前用過的單元格。所以你改變了一切,你不想像未知的其他行一樣。

1

在cellForRowAtIndexPath中,可以使用indexPath獲取正確的值。 我看不到你的代碼,所以如果你只是用了一些像1,2,3,4,等等,你可以做 例子: cell.label.text = indexPath.row + 1

,或者如果您使用的是數組,你可以這樣做以下

cell.label.text = array[indexPath.row] 

希望這會有所幫助!

相關問題