2014-12-01 45 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