Взять информационный писатель в ячейку, когда кнопка на ячейке нажата - PullRequest
4 голосов
/ 20 октября 2019
- (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. Есть ли способ добиться этого?

1 Ответ

1 голос
/ 20 октября 2019

используйте 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;
}
...