Мне нужна помощь, чтобы выяснить, какой переключатель типа аксессуара был изменен.
У меня есть список игр в UITableView. Я добавил UISwitch в качестве типа аксессуара, и каждый коммутатор реагирует на прикосновение, изменяя свое состояние и уведомляя приемник своих действий.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell =[tableView dequeueReusableCellWithIdentifier:@"cell"];
if(nil == cell){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"cell"]autorelease];
}
Games *games=[appDelegate.hidden objectAtIndex:indexPath.row];
cell.textLabel.text = games.gameName;
//the switch is defined here, as is its target-action
testSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(0.0, 0.0, 74.0, 27.0)];
[testSwitch addTarget:self action:@selector(button_Clicked:)
forControlEvents:UIControlEventValueChanged];
//If I used the tags
testSwitch.tag=indexPath.row;
[testSwitch setOn:YES];
cell.accessoryView=testSwitch;
[games release];
return cell;
}
И цель-действие: см. Комментарии
- (void)button_Clicked:(id)sender{
UISwitch* switchControl = sender; //if I make this an int, the switches
//have different numbers. If it's cast as a string, it's gobbledegook.
//This always returns 0. I can't tell it it's not getting anything (ie it's
// nil), or if the row always comes up 0.
UITableViewCell *cell = (UITableViewCell *)[sender superview];
NSIndexPath *x=[table indexPathForCell:cell];
NSLog(@"%i",x.row);
}
Я вижу две потенциальные проблемы. Во-первых, мне нужно сделать переключатель для каждой строки с уникальным именем. Во-вторых, я неправильно получаю камеру.
Если кто-то может увидеть мои ошибки, любая помощь приветствуется.
Я просмотрел все, что могу найти в stackoverflow, и несколько страниц в 5 или 6 различных поисковых запросах в Google, два приведенных выше примера в методе target-action являются лучшими, которые я нашел.