【问题标题】:Take Information Writter In A Cell When Button On A Cell Tapped当点击单元格上的按钮时,将信息写入单元格中
【发布时间】:2024-01-01 12:27:01
【问题描述】:
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {

    static NSString *cellId = @"cell";
    CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:cellId];

    if (cell == nil)
    {
        cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
    }

    self.likeBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, cell.frame.origin.y+100, 80, 20)];
    [self.likeBtn  setTitle:@"beğen" forState:UIControlStateNormal];
    self.likeBtn.backgroundColor = [UIColor blackColor];
    [cell addSubview:likeBtn];

    cell.titleLabel.text = [dataArr[indexPath.row] objectForKey:@"title"];
    cell.infoLabel.text = [dataArr[indexPath.row] objectForKey:@"description"];
    likeBtn.tag = indexPath.row;
    //NSLog(@"tag %d", likeBtn.tag);
    [likeBtn addTarget:self action:@selector(likeBtnTapped:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}
- (void) likeBtnTapped:(id)sender {
    NSLog(@"Like Button Tapped");
    NSLog(@"tag %d", likeBtn.tag);

    likeDataArray = [[NSMutableArray alloc] init];

        [[[self.ref child:@"user"] child:[FIRAuth auth].currentUser.uid] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
            if(snapshot.exists){
                self->likeDataArray = snapshot.value[@"liked"];
                NSLog(@"logged: %@", self->likeDataArray);
            }


        } withCancelBlock:^(NSError * _Nonnull error) {
            NSLog(@"%@", error.localizedDescription);

        }];

}

这是我的代码。我在每个单元格上都有一个赞按钮。当点击按钮时,我想获取单元信息并写入 Firebase 数据库。 我尝试为每个喜欢的按钮定义一个标签,但如果有 8 个按钮(或单元格),例如它将所有按钮标签定义为 8。 有没有办法做到这一点?

【问题讨论】:

    标签: ios objective-c uitableview cell


    【解决方案1】:

    使用 sender.tag 而不是 likeBtn。

    - (void) likeBtnTapped:(UIButton *)sender {
        NSLog(@"Like Button Tapped");
        NSLog(@"tag %d", sender.tag);
    
        likeDataArray = [[NSMutableArray alloc] init];
    
            [[[self.ref child:@"user"] child:[FIRAuth auth].currentUser.uid] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
                if(snapshot.exists){
                    self->likeDataArray = snapshot.value[@"liked"];
                    NSLog(@"logged: %@", self->likeDataArray);
                }
    
    
            } withCancelBlock:^(NSError * _Nonnull error) {
                NSLog(@"%@", error.localizedDescription);
    
            }];
    
    }
    

    或更改 cellForRowAtIndexPath 方法

    - (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
        static NSString *cellId = @"cell";
        CustomTableViewCell *cell = (CustomTableViewCell*)[tableView dequeueReusableCellWithIdentifier:cellId];
    
        if (cell == nil)
        {
            cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
        }
    
        UIButton *likeButton  = [[UIButton alloc] initWithFrame:CGRectMake(10, cell.frame.origin.y+100, 80, 20)];
        [likeButton setTitle:@"beğen" forState:UIControlStateNormal];
        self.likeBtn.backgroundColor = [UIColor blackColor];
        [cell addSubview:likeButton];
    
        cell.titleLabel.text = [dataArr[indexPath.row] objectForKey:@"title"];
        cell.infoLabel.text = [dataArr[indexPath.row] objectForKey:@"description"];
        likeButton.tag = indexPath.row;
        [likeButton addTarget:self action:@selector(likeBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
    
        return cell;
    }
    

    【讨论】: