Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd custom action:@selector to UISwitch on TableViewCell
    primarykey
    data
    text
    <p>I have TableCellViewController for managing cells in my UITableView. Each cell has a label na UISwitch (dictTypeSwitch). A want to assign method to switch events so I can save the state of button.</p> <p>So far I've done this:</p> <p>assign setState function to object:</p> <pre><code>[cell.dictTypeSwitch addTarget:self action:@selector(setState:) forControlEvents:UIControlEventTouchUpInside]; </code></pre> <p>function for handling event:</p> <pre><code>- (void)setState:(id)sender { BOOL state = [sender isOn]; NSString *rez = state == YES ? @"YES" : @"NO"; NSLog(rez); } </code></pre> <p>From sender I get UISwitch object from which I can get state.</p> <p>So far everything is ok.</p> <p>But if I want to save the state of UISwitch, I have to get rowIndex for this cell. How can I achieve this?</p> <pre><code>The cells are made inside function cellForRowAtIndexPath. Se the code bellow: // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"DictionariesTableCellViewController"; DictionariesTableCellViewController *cell = (DictionariesTableCellViewController *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"DictionariesTableCellViewController" owner:nil options:nil]; for (id currentObject in topLevelObjects) { if ([currentObject isKindOfClass:[UITableViewCell class]]) { cell = (DictionariesTableCellViewController *) currentObject; break; } } } // Configure the cell... cell.dictTypeLabel.text = [NSString stringWithFormat:@"row - %i",indexPath.row]; [cell.dictTypeSwitch addTarget:self action:@selector(setState:) forControlEvents:UIControlEventTouchUpInside]; return cell; } </code></pre> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    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. 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