- (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);
}];
}
Here's my code. I have a like button on every cell. When button tapped I want to get the cell information and write to Firebase Database. I try to define a tag for every like button, but if there are 8 button(or cell) e.g it define all the buttons tag as 8. Is there a way to achive this?
use sender.tag instead of 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);
}];
}
or change cellForRowAtIndexPath method
- (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;
}