[轻按单元格上的按钮时在单元格中获取信息提示

问题描述 投票:0回答:1
- (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个回答
0
投票

使用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方法

© www.soinside.com 2019 - 2024. All rights reserved.