1

i wants just know.... to add buttons in each cell in table row.....programmatically in runtime and also wants identify the button when clicked....

i write little code..here....

//---insert individual row into the table view---


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[NSString   stringWithFormat:@"Cell %i",indexPath.section]];

    if (cell == nil) {

        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[NSString stringWithFormat:@"Cell %i",indexPath.section]] autorelease];

    }


    NSLog(@"i am here in table function..........now");


    //cell.textLabel.text = [data objectAtIndex: indexPath.row];

    search_items *pro = [searchCount objectAtIndex:[indexPath row]];

    cell.textLabel.text = pro.s_showroomName;



    UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

        [aButton setTag:[indexPath row]];

        [aButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];


    [cell.contentView addSubview:aButton];

        return cell;
}

-(void)buttonClicked:(UIButton*)sender {
    int tag = sender.tag;

///and rest code here.....

}

this code not work properly....:) please give me solution :)

thanks in advance.....please give me any tutorial also..

Vladimir
  • 170,431
  • 36
  • 387
  • 313
GauravBoss
  • 140
  • 2
  • 4
  • 13
  • possible duplicate of [Detecting which UIButton was pressed in a UITableView](http://stackoverflow.com/questions/1802707/detecting-which-uibutton-was-pressed-in-a-uitableview) – Vladimir Oct 20 '11 at 11:53
  • 1
    Which 'code not work properly'. Can you see the buttons on the Table View cells. If not, try setting the button frame to x=0, y=0, w=100, h=30. Can you also see the populated table (table cells with text)? – Sahil Khanna Oct 20 '11 at 11:56
  • @sahil.... i see populated table with text ..i fetch the records from database using NSMuttable array searchCount.... (You are right button frame to x=0, y=0, w=100, h=30) ...thanks brother – GauravBoss Oct 20 '11 at 12:20

2 Answers2

1

set the frame for your button like this..

  aButton.frame = CGRectMake(0, 0,150,44);   
Aravindhan
  • 15,608
  • 10
  • 56
  • 71
0

chck the question you may get some help runtime adding button and calling methods on them. iPhone: Add UIButton's horizontally with scrollbar provision in a View or TableView?

Community
  • 1
  • 1
B25Dec
  • 2,301
  • 5
  • 31
  • 54