Home > DeveloperSection > Forums > iOS How to get button text from tableview cell
Felix Pickles
Felix Pickles

Total Post:24

Points:168
Posted on    July-23-2015 12:12 AM

 iPhone iPhone  Ios  Objective C 
Ratings:


 1 Reply(s)
 538  View(s)
Rate this:

I am using simple tableview and i have add button in every cell , The Problem is how to get button text from cell number second and any other cell number get button text i am using this code but its not working

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];

UITableViewCell *cell = [tbl_view cellForRowAtIndexPath:indexPath]; 

for( UITableViewCell *getview in cell.subviews)

{

       if([getview isKindOfClass:[UIButton class]])

        {

             NSLog(@"sdfsdfsd");

             // UIButton *button = (UIButton *);

        }

}

This code is set button in tableview cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:

(NSIndexPath *)indexPath { 

 

      static NSString *simpleTableIdentifier = @"SimpleTableItem";

      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:

simpleTableIdentifier];

      UILabel *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 5, 250,20)];

      if (cell == nil) { 

          cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:

simpleTableIdentifier];

         cell = [[UITableViewCell alloc] init];

      } 

      cell.backgroundColor=[UIColor clearColor];

    UIButton *button_chaeckbox = [UIButton buttonWithType:UIButtonTypeCustom];

      button_chaeckbox.backgroundColor=[UIColor clearColor];

      button_chaeckbox.frame = CGRectMake(10, 10, 15, 15);

 

      if( [checkedArray containsObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row]])

      {

          [button_chaeckbox setBackgroundImage:[UIImage imageNamed:

@"checked_checkbox.png"] forState:UIControlStateNormal];

      }else{

         [button_chaeckbox setBackgroundImage:[UIImage imageNamed:

@"empty_box_b.png"] forState:UIControlStateNormal];

      }

 [button_chaeckbox setTitle:@"creaButtonname" forState:UIControlStateNormal];

      button_chaeckbox.tag=indexPath.row;

      [button_chaeckbox addTarget:self action:@selector(checkboxAction:)

      forControlEvents:UIControlEventTouchUpInside];

      [cell addSubview:button_chaeckbox];

}

Please give me solution , i have try this code in ios 7 and ios 8 this is not working.



Tarun Kumar

Total Post:213

Points:1635
Posted on    July-23-2015 9:08 AM

Use this code for reuse cells in table view

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:

(NSIndexPath *)indexPath {
     static NSString  *simpleTableIdentifier = @"SimpleTableItem";
     UITableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier: simpleTableIdentifier];

     if (cell == nil) {
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
         cell=[[UITableViewCell alloc] init];
         UILabel  *fromLabel = [[UILabel alloc]initWithFrame:CGRectMake(60, 5, 250,20)];
         cell.backgroundColor=[UIColor clearColor];
         UIButton *button_chaeckbox = [UIButton buttonWithType:UIButtonTypeCustom];
         button_chaeckbox.backgroundColor=[UIColor clearColor];
         button_chaeckbox.frame = CGRectMake(10, 10, 15, 15);
         [button_chaeckbox addTarget:self  action:@selector(checkboxAction:)
         forControlEvents:UIControlEventTouchUpInside];
         [cell addSubview:button_chaeckbox];
         button_chaeckbox.tag=1001;
     }

     if([checkedArray containsObject:[NSString stringWithFormat:@"%ld",(long) indexPath.row]]){
         [button_chaeckbox setBackgroundImage:[UIImage  imageNamed:@"checked_checkbox.png"]                                                                                                               forState:UIControlStateNormal];
     } else {
     [button_chaeckbox setBackgroundImage:[UIImage imageNamed:@"empty_box_b.png"]                                                                              forState:UIControlStateNormal];
     }
}

Use this code for get button from cell

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];

UITableViewCell *cell = [tbl_view cellForRowAtIndexPath:indexPath];

UIButton *button_chaeckbox=(UIButton*)[cell viewWithtag:1001];

 

 

 


Modified On Jul-23-2015 09:14:53 AM

Don't want to miss updates? Please click the below button!

Follow MindStick