检测哪个按钮需要更改单元格中的文本

时间:2014-12-01 16:25:17

标签: ios xcode uibutton tags tableviewcell

我喜欢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];值会转到错误的单元格。

如何检测需要设置单元格值中的哪个按钮?我如何使用我的标签?

2 个答案:

答案 0 :(得分:1)

您无法在like:方法中设置按钮标题,即:

[likeButton setTitle:strValue forState:UIControlStateNormal];

(1)因为您之后正在重新加载表格,因此即使您没有重新加载表格数据,也会手动触发对cellForRowAtIndexPath:和(2)的调用,即使滚动也会触发致电cellForRowAtIndexPath,因此您的小区将根据cellForRowAtIndexPath:的信息进行填充。

现在,您的cellForRowAtIndexPath:方法使用以下行设置标题:

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

这意味着为了让like:动作触发标题更改,您必须更改like:中的[tmpDict objectForKey:@" likes"]值。由于您正在使用临时字典并且从未将字典添加回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:来电时,您都会在另一个按钮的顶部添加一个按钮。这可能会给你前进带来麻烦。

答案 1 :(得分:0)

您的手机可能会重复使用按钮,标签可能会混乱。 尝试在tableView中刷新bouttons的标签:willDisplayCell:forRowAtIndexPath:method。