4

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;        
}
4
  • Are you sure the action is performed on all the labels or on the label that is created last?
    – Exploring
    Jan 24, 2013 at 7:08
  • @sanjitshaw yes the action is performed on the label is created last
    – Charlie
    Jan 24, 2013 at 7:10
  • I just want to know small thing how many sections do you have?
    – Exploring
    Jan 24, 2013 at 7:13
  • Not the number of cells I'm asking for number of sections.
    – Exploring
    Jan 24, 2013 at 7:17

5 Answers 5

4

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
}
9
  • this code works fine, bt when i scroll down the table then the state is lost the second cell shows the like count label
    – Charlie
    Jan 24, 2013 at 7:45
  • Thank you very much, one more problem in this,how can i have seprate value for each cell that, each cell have its own like value, now only one value is there for all.. have u understood my problem
    – Charlie
    Jan 24, 2013 at 8:48
  • I think you have an array for like values so use that array to display the like values. I've done that code for a sample only.
    – Exploring
    Jan 24, 2013 at 8:54
  • Can u please modify this code for like array, i m unable to do it
    – Charlie
    Jan 24, 2013 at 9:28
  • I'd like to be sure that you have an array for the like values and how are the contents of that array(if any) just provide me as an example.
    – Exploring
    Jan 24, 2013 at 9:35
3

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.

1
-(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

3
  • Terl- it still showing the like label on the last cell when i click on the like button on first cell
    – Charlie
    Jan 24, 2013 at 7:36
  • did you copy this line [likeBtn addTarget:self action:@selector(likeFn::) forControlEvents:UIControlEventTouchUpInside]; as it is? Jan 24, 2013 at 8:14
  • Yes, and my probelm has been solved by sanjit's answer bt when i scroll the table the state is lost of the labels
    – Charlie
    Jan 24, 2013 at 8:19
0

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.


}
0

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;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.