2015-06-22 57 views
2

我有UITableViewCell的具有目標的功能這樣的按鈕:爲什麼uitableviewcell在滾動時發生變化?

likeButton?.addTarget(self, action: "likeButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside) 

設置我的按鈕標題是這樣的函數內部:

sender.setTitle("\(addedLikeCount) Likes", forState: UIControlState.Normal) 

但每當我滾動視圖向上或向下,我的按鈕標題更改爲默認值。爲什麼會發生?有沒有什麼辦法可以解決這個問題,而無需重新加載表?

隨時給我任何意見,在迅速或客觀的c。無所謂。

UPDATE

所以我做了下面的代碼在我的功能:

self.likeArray.replaceObjectAtIndex(index!, withObject: addedLikeCount) 

sender.setTitle("\(self.likeArray[index!]) Likes", forState: UIControlState.Normal) 

這對我的UITableViewCell:

var totalLike = likeArray[indexPath.row] as? String 

currentLikeCount = totalLike 

likeButton?.setTitle("\(totalLike)", forState: UIControlState.Normal) 

它的工作,但是當我滾動絲毫不差成爲再次默認

+0

因爲細胞正在被重用,因此刷新至下'-cellForRowAtIndexPath',解決原來的設置是有一個全局變量,然後下重裝安裝'-cellForRowAtIndexPath' – 0yeoj

+0

我該怎麼辦,沒有reloaddata? –

+0

獲得superview(單元格)被按下的'likeButton'是,然後更改/設置標題,但不重新加載表並不滾動否則它將被重用並再次刷新,這是tableview的自然行爲(用於內存處理)..在你的情況意思..這是你唯一的選擇.. :) – 0yeoj

回答

0

As Oyeo j評論

單元正在改變,因爲「該單元正在被重用並因此被刷新到cellForRowAtIndexPath下的原始設置,解決方法是在全局變量上設置,然後在cellForRowAtIndexPath下重新加載時設置。

對於您所需的任務,您必須將選定的單元格索引存儲在某個位置,即您的someIndex單元格上的按鈕具有Likes標題。

中.H

NSMutableArray likeArray;

在.M

爲目標C

viewDidLoad

likeArray=[[NSMutableArray alloc]init];

cellForRowAtIndexPath校驗

if ([likeArray containObject:[NSNumber numberWithInteger:indexPath.row]]) 
{ 
    [button setTitle:@"Likes", forState: UIControlStateNormal]; 
} 
else 
{ 
    [button setTitle:@"Some Other Title", forState: UIControlStateNormal]; 
} 
didSelectRowAtIndexPath

-

NSNumber *cellIndex=[NSNumber numberWithInteger:indexPath.row]; 
if(![likeArray containObject:cellIndex]) 
{ 
    [likeArray addObject:cellIndex]; 
} 
[table reloadData]; 
+0

我編輯了我的問題,我已經嘗試過你的解決方案,但它不適合我。 –

+0

答覆已更新。 –

+0

它應該與Check Box Accessory按鈕相同。 –

0

創建該檢查哪一行的標題被改變,並且副versa.For例如,功能

-(BOOL)checkForSelectedRow:(NSIndexPath *)path 

立即創建陣列,其用於存儲哪個按鈕的標題改變,所以無論何時按下按鈕ü將它添加到您的likeButtonTapped

現在

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

if([self checkForSelectedRow:indexPath]){ 
//change the title of button 

} 
else{ 
//default of button 
} 
1

你在代碼中使用呢?

tableView.dequeueReusableCellWithIdentifier("reuseIdentifer") 

如果是,則必須保存每個單元格的狀態。

因爲每當你上下滾動時,TableView都會返回屏幕以外的前一個單元格。

你需要的是將小區設置的新狀態是什麼對應indexPathcellForRowAtIndexPath方法是這樣的

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

    if let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifer") as? UITableViewCell{ 

     cell.title = titles[indexPath.row] 
     return cell 

    } 
    ... 
    ... 
} 
+0

它給了我一個錯誤,xcode無法找到返回,如果我把回報裏面,如果語句 –

+0

把返回UITableViewCell()在函數的結尾 – nRewik

2

泰伯維細胞重新加載每次它會滾動後,屏幕上顯示。 你必須爲你的你故事情節,或者提供可重複使用的標識符XIB Objective-C的變種:

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"your_cell_identifier_on_storyboard_or_cell_xib"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (!cell) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    /// datasource code here 

    return cell; 

} 

如果你想,以儲存所有的上下文具體indexpath作爲對您的問題 - 來容納所有按鈕的標題,使用

dequeueReusableCellWithIdentifier: forIndexPath: 

,而不是

dequeueReusableCellWithIdentifier: 

警告:離隊細胞對特定indexpath可能會降低性能

相關問題