Reputation: 187
In UITableViewCell there are different buttons and when I click on any button its action is being performed on all the cells, but I want that when I press the button then the action will be shown on the label of the same cell, I know I need to put them in array but how...?
when I click on the button one value is being incremented and it should be shown on the label of the same cell here is the code:-
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
likeShow=[[UILabel alloc]initWithFrame:CGRectMake(0, 160, 80, 20)];
likeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[likeBtn addTarget:self action:@selector(likeFn) forControlEvents:UIControlEventTouchUpInside];
likeBtn.frame=CGRectMake(0, 110, 90, 50);
[likeBtn setTitle:@"Like" forState:UIControlStateNormal];
[likeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cell addSubview:likeBtn];
[cell addSubview:likeShow];
return cell;
}
This is the action of the button
-(void)likeFn{
NSString *str;
NSMutableString *mystring=[NSMutableString string];
likeCount++;
str =[NSString stringWithFormat:@"%d",likeCount];
NSString *world = @"Like";
NSString *helloWorld = [world stringByAppendingString:str];
likeShow.text=helloWorld;
}
Upvotes: 4
Views: 2500
Reputation: 935
Try the below code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:@"Cell-%d",indexPath.row];
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//[cell clearsContextBeforeDrawing];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
int lbltag = 1000;
UIButton *likeBtn = nil;
UILabel *likeShow = nil;
if ([cell viewWithTag:lbltag])
{
likeShow = (UILabel*)[cell viewWithTag:lbltag];
}
else
{
likeShow=[[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 20)] autorelease];
likeShow.text = [NSString stringWithFormat:@"%d likes",[[_marrTest objectAtIndex:indexPath.row] intValue]];
likeShow.tag = lbltag;
[cell addSubview:likeShow];
}
if ([cell viewWithTag:indexPath.row+1])
{
likeBtn = (UIButton*)[cell viewWithTag:indexPath.row+1];
}
else
{
likeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
likeBtn.tag = indexPath.row+1;
[likeBtn addTarget:self action:@selector(likeFn:) forControlEvents:UIControlEventTouchUpInside];
likeBtn.frame=CGRectMake(90, 0, 90, 50);
[likeBtn setTitle:@"Like" forState:UIControlStateNormal];
[likeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cell addSubview:likeBtn];
}
return cell;
}
-(void)likeFn:(UIButton*)btnClicked
{
NSString *strLikes = [_marrTest objectAtIndex:btnClicked.tag-1];
int likeCount = [strLikes intValue] + 1;
[_marrTest replaceObjectAtIndex:btnClicked.tag-1 withObject:[NSString stringWithFormat:@"%d",likeCount]];
NSIndexPath *selectedIndexPath = [NSIndexPath indexPathForRow:btnClicked.tag-1 inSection:0];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:selectedIndexPath];
UILabel *requiredLabel = (UILabel*)[cell viewWithTag:1000];
NSString *str = requiredLabel.text;
//str = [str stringByAppendingFormat:@"selected %@", str];
requiredLabel.text = @"";
requiredLabel.text = [NSString stringWithFormat:@"%d likes",[[_marrTest objectAtIndex:btnClicked.tag-1] intValue]];
//do what ever you want with the label
}
Upvotes: 4
Reputation: 2343
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
likeShow=[[UILabel alloc]initWithFrame:CGRectMake(0, 160, 80, 20)];
likeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[likeBtn addTarget:self action:@selector(likeFn::) forControlEvents:UIControlEventTouchUpInside];
likeBtn.frame=CGRectMake(0, 110, 90, 50);
[likeBtn setTitle:@"Like" forState:UIControlStateNormal];
[likeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cell.contentView addSubview:likeBtn];
[cell.contentView addSubview:likeShow];
return cell;
}
-(void)likeFn:(UIButton *)sender :(UIEvent *)event{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.view];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:currentTouchPosition];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
NSString *str;
NSMutableString *mystring=[NSMutableString string];
likeCount++;
str =[NSString stringWithFormat:@"%d",likeCount];
NSString *world = @"Like";
NSString *helloWorld = [world stringByAppendingString:str];
for(id subView in cell.contentView.subviews){
{
if([subView isKindOfClass:[UILabel class]]){
UILabel *tmpLabel = (UILabel *)subView;
tmpLabel.text=helloWorld;
}
}
}
Try this code it may help you
Upvotes: 1
Reputation: 320
Check with this code.This may helpful for you.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = @"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
[cell clearsContextBeforeDrawing];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
likeShow=[[UILabel alloc]initWithFrame:CGRectMake(0, 160, 80, 20)];
likeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[likeBtn addTarget:self action:@selector(likeFn) forControlEvents:UIControlEventTouchUpInside];
likeBtn.frame=CGRectMake(0, 110, 90, 50);
[likeBtn setTitle:@"Like" forState:UIControlStateNormal];
[likeBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[cell addSubview:likeBtn];
[cell addSubview:likeShow];
return cell;
}
USE didSelectRowAtIndexPath method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
return;
}
Upvotes: 0
Reputation: 424
Create cell in separate nib file. add button and label for for that cell. Also create a view class for that cell and set class for cell in nib file as class you created. Make outlet form nib to class file. Also create action for button and implement action. and use that cell in your table view.
Upvotes: 3
Reputation: 4246
try setting tags as:
likeBtn.tag = indexPath.row;
likeShow.tag = indexPath.row;
and in the likeFn
:
- (void)likeFn: (id)sender {
UIButton * btn = (UIButton*)sender;
//check if the btn tag and label tag are same.
}
Upvotes: 0