2014-12-01 34 views
0

我有一個按鈕,在tableviewcell檢測哪個按鈕需要修改文本在細胞

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 


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

     DBImageView *imageView = [[DBImageView alloc] initWithFrame:(CGRect){ 0, 0, 320, 320 }]; 
     [imageView setPlaceHolder:[UIImage imageNamed:@"Placeholder"]]; 
     [imageView setTag:101]; 
     [cell.contentView addSubview:imageView]; 
    } 


    NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row]; 
    [(DBImageView *)[cell viewWithTag:101] setImageWithPath:[tmpDict objectForKey:@"thumb_link"]]; 


    likeButton = [UIButton buttonWithType:UIButtonTypeCustom] ; 
    [likeButton setFrame:CGRectMake(cell.frame.size.width-60,280,81,33)]; 
    likeButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter; 
    likeButton.contentEdgeInsets = UIEdgeInsetsMake(3, 2, 0, 0); 
    [likeButton.titleLabel setFont:[UIFont fontWithName:@"Trebuchet MS" size:16]]; 
    [likeButton setBackgroundImage:[UIImage imageNamed:@"heart.png"] forState:UIControlStateNormal]; 
    [likeButton setBackgroundImage:[UIImage imageNamed:@"heart_pressed.png"] forState:UIControlStateSelected]; 
    [cell addSubview:likeButton]; 


    // like button 
    likeButton.selected = ![likeButton isSelected]; 
    NSInteger defaultKey2 = [[NSUserDefaults standardUserDefaults] integerForKey:[tmpDict objectForKey:@"id"]]; 
    if (defaultKey2 == 0) 
    { 
     //[likeButton setTitleColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0] forState:UIControlStateNormal]; 
     [likeButton setSelected:NO]; 
    } 
    else if (defaultKey2 == 1) 
    { 
     //[likeButton setTitleColor:[UIColor colorWithRed:229/255.0 green:85/255.0 blue:140/255.0 alpha:1.0] forState:UIControlStateNormal]; 
     [likeButton setSelected:YES]; 
    } 


    [likeButton setTitle:[tmpDict objectForKey:@"likes"] forState:UIControlStateNormal]; 
    [likeButton addTarget:self action:@selector(like:) forControlEvents: UIControlEventTouchUpInside]; 
    [likeButton setTag:indexPath.row]; 

    return cell; 

} 

這是我的行動:

-(void)like:(id) sender{ 
    UIButton *likebtn = (UIButton *)sender; 
    NSDictionary *tmpDict = myObject[likebtn.tag]; 
    NSInteger defaultKey2 = [[NSUserDefaults standardUserDefaults] integerForKey:[tmpDict objectForKey:@"id"]]; 
    if (defaultKey2 == 0) 
    { 
     //Increase value 
     int varTemp = [[tmpDict objectForKey:@"likes"] intValue]+1; 
     NSString *strValue = [NSString stringWithFormat:@"%d", varTemp]; 
     [likeButton setTitle:strValue forState:UIControlStateNormal]; 
     NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
     [prefs setInteger:1 forKey:[tmpDict objectForKey:@"id"]]; 
     [[NSUserDefaults standardUserDefaults]synchronize]; 
    } 
    if (defaultKey2 == 1) 
    { 
     //Decrease value 
     int varTemp = [[tmpDict objectForKey:@"likes"] intValue]-1; 
     NSString *strValue = [NSString stringWithFormat:@"%d", varTemp]; 
     [likeButton setTitle:strValue forState:UIControlStateNormal]; 
     NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
     [prefs setInteger:0 forKey:[tmpDict objectForKey:@"id"]]; 
     [[NSUserDefaults standardUserDefaults]synchronize];  
    } 
    NSIndexPath *tmpIndexpath=[NSIndexPath indexPathForRow:likebtn.tag inSection:0]; 
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:tmpIndexpath, nil] withRowAnimation:UITableViewRowAnimationNone]; 
} 

當我嘗試在細胞 設置標題爲我的按鈕[likeButton setTitle:strValue forState:UIControlStateNormal];值錯誤的單元格。

如何檢測單元值中的哪個按鈕需要設置?我如何使用我的標籤?

+1

你是什麼意思「價值錯誤細胞」?你可以請你發佈你的代碼從cellForRowAtIndexPath? – 2014-12-01 16:27:58

+0

我需要增加或減少喜歡的值,當我按下按鈕時,值錯誤的單元格更改。 – 2014-12-01 16:29:48

+0

好吧,有幾件事情......在你的'like:'方法裏,「likeButton」來自條件之內嗎?因爲你在方法開始時聲明瞭「likebtn」。二,你不能這樣設置按鈕標題。它必須在cellForRowAtIndexPath中設置。一旦你解釋了「likeButton」的聲明,我會很快發佈一個建議。 – 2014-12-01 16:41:11

回答

1

您不能設置按鈕標題這樣在你like:方法,即:

[likeButton setTitle:strValue forState:UIControlStateNormal]; 

(1)因爲你重裝右表事後從而觸發手動調用cellForRowAtIndexPath:和(2 ),即使您沒有重新載入表格數據,即使滾動也會觸發cellForRowAtIndexPath的調用,因此您的單元格將根據cellForRowAtIndexPath:的信息進行填充。

目前的情況是,你cellForRowAtIndexPath:方法是使用下面的行設置標題:

[likeButton setTitle:[tmpDict objectForKey:@"likes"] forState:UIControlStateNormal]; 

這意味着,爲了使like:動作來觸發標題的變化,你必須改變[ tmpDict objectForKey:@「likes」]值爲like:。由於您使用的是臨時字典,並且絕不會將字典添加回myObject數組中,所以您沒有更改最終決定您的按鈕標題的值爲cellForRowAtIndexPath:。所以你必須相應地更新myObject。這裏是我的代碼的建議:

-(void)like:(id)sender { 

    UIButton *likebtn = (UIButton *)sender; 
    NSMutableDictionary *tmpDict = myObject[likebtn.tag]; 
    NSInteger defaultKey2 = [[NSUserDefaults standardUserDefaults] integerForKey:[tmpDict objectForKey:@"id"]]; 

    if (defaultKey2 == 0) 
    { 
     //Increase value 
     int varTemp = [[tmpDict objectForKey:@"likes"] intValue]+1; 

     // Then update myObject 
     [tmpDict setObject:[NSString stringWithFormat:@"%d", varTemp] forKey:@"likes"]; 
     [myObject replaceObjectAtIndex:likebtn.tag withObject:tmpDict]; 

     NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
     [prefs setInteger:1 forKey:[tmpDict objectForKey:@"id"]]; 
     [[NSUserDefaults standardUserDefaults]synchronize]; 
    } 
    if (defaultKey2 == 1) 
    { 
     //Decrease value 
     int varTemp = [[tmpDict objectForKey:@"likes"] intValue]-1; 

     // Then update myObject 
     [tmpDict setObject:[NSString stringWithFormat:@"%d", varTemp] forKey:@"likes"]; 
     [myObject replaceObjectAtIndex:likebtn.tag withObject:tmpDict]; 


     NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
     [prefs setInteger:0 forKey:[tmpDict objectForKey:@"id"]]; 
     [[NSUserDefaults standardUserDefaults]synchronize];  
    } 

    NSIndexPath *tmpIndexpath=[NSIndexPath indexPathForRow:likebtn.tag inSection:0]; 
    [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:tmpIndexpath, nil] withRowAnimation:UITableViewRowAnimationNone]; 

} 

編輯:也有現在是一個很大的問題,我已經看到你的cellForRowAtIndexPath:方法。你已經將該按鈕聲明爲一個類變量? (1)不要這樣做。保持它的地方,例如:

UIButton *likeButton = [UIButton buttonWithType:UIButtonTypeCustom] ; 

(2)你重用你的細胞,但每次添加一個新的按鈕。所以你要在另一個頂部添加一個按鈕,每個按鈕都要撥打cellForRowAtIndexPath:。這可能會導致你前進的問題。

+0

***由於未捕獲的異常'NSInvalidArgumentException'終止應用程序,原因:' - [__ NSCFNumber長度]:無法識別的選擇器發送到實例0x15768450' – 2014-12-01 17:14:18

+0

@PavelKaljunen嗯...我不完全確定這是爲什麼。但是我已經更改了代碼並將其分成兩個獨立的步驟。它現在工作嗎? – 2014-12-01 17:20:18

+0

'NSDictionary'沒有可見的@interface聲明選擇器'setObject:forKey:' – 2014-12-01 17:22:27

0

您的手機可能會重複使用按鈕,並且標記可能會混亂。 嘗試在tableView:willDisplayCell:forRowAtIndexPath:方法中刷新bouttons的標籤。

+0

目前我有reuseIdentifier:無 – 2014-12-01 16:55:37