Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following code gives three buttons and no jaggy scrolling. Using tags, you can reset the text of the buttons depending on the row. This is illustrated for button 1 (which adjusts its title according to the row number). The <code>button1Pressed:</code> method illustrated figures out what row the button press came from. Hope this will be helpful.</p> <pre><code>- (UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString* cellIdentifier = @"Cell"; // see if there's a cell available to recylce UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (!cell) { // there's no cell to recycle, so make a new one // add three buttons to it and tag them so we can alter their contents later cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellIdentifier]; UIButton* button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button1 setTitle:@"Button 1" forState:UIControlStateNormal]; [button1 setFrame:CGRectMake(4.0, 15.0, 110, 30.0)]; [button1 setTag:101]; [button1 addTarget:self action:@selector(button1Pressed:) forControlEvents:UIControlEventTouchUpInside]; [[cell contentView] addSubview:button1]; UIButton* button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button2 setTitle:@"Button 2" forState:UIControlStateNormal]; [button2 setFrame:CGRectMake(120.0, 15.0, 80.0, 30.0)]; [button2 setTag:102]; [button1 addTarget:self action:@selector(button2Pressed:) forControlEvents:UIControlEventTouchUpInside]; [[cell contentView] addSubview:button2]; UIButton* button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [button3 setTitle:@"Button 3" forState:UIControlStateNormal]; [button3 setFrame:CGRectMake(210, 15.0, 80.0, 30.0)]; [button3 setTag:103]; [button3 addTarget:self action:@selector(button3Pressed:) forControlEvents:UIControlEventTouchUpInside]; [[cell contentView] addSubview:button3]; } // either on a recycled cell or on the cell just created, set the contents UIButton* button1 = (UIButton*)[[cell contentView] viewWithTag:101]; [button1 setTitle: [NSString stringWithFormat:@"Button 1 - %d", [indexPath row]] forState:UIControlStateNormal]; cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; } - (void) button1Pressed: (UIButton*) button { CGPoint buttonCentre = [button convertPoint:[button center] toView:[self tableView]]; NSLog(@"Button 1 Pressed on row %d", [[[self tableView] indexPathForRowAtPoint:buttonCentre] row]); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload