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

标签 ios xcode uibutton tags uitableview

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

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

最佳答案

您不能在 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: 时,您都会在另一个按钮之上添加一个按钮。这可能会给您的前进带来麻烦。

关于ios - 检测哪个按钮需要更改单元格中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27232697/

相关文章:

ios - 如何使用 NSLayoutConstraint 限制 UIButton 高度

ios - UIButton 和 @3x 图像(使用图像资源)

ios - 在 UIButton IOS 上创建一系列动画

ios - Swift:尝试附加随机数组元素时获取 EXC_BAD_INSTRUCTION

objective-c - 在类函数中使用@synchronized(self) { ... }

ios - 无法构建模块基础

ios - 核心数据加密类

c++ - Xcode 上的 SDL2 代码设计问题

ios - 我可以使用 AVCaptureSession 从文件中读取视频吗?

c# - 以编程方式创建按钮单点触控