我有一個帶有切換開關輔助視圖的桌面視圖。我指定了該部分和行,並且難以確定哪一行被切換。我使用toggleSwitch.tag來抓取indexRow,但是因爲我的indexRow是indexPath.section的一部分,所以我不知道如何判斷我切換了哪一行。如何判斷哪些行切換開關已更改
下面是代碼:
- (UITableViewCell *)tableAlert:(SBTableAlert *)tableAlert cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
Category *cat = [allCategories objectAtIndex:indexPath.section];
Subject *sub = [cat.subjects objectAtIndex:indexPath.row];
cell = [[[SBTableAlertCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
UISwitch *toggleSwitch = [[UISwitch alloc] init];
cell.accessoryView = [[UIView alloc] initWithFrame:toggleSwitch.frame];
[cell.accessoryView addSubview:toggleSwitch];
cell.textLabel.text =sub.title;
cell.detailTextLabel.text = sub.category_title;
if (sub.active==1){
[toggleSwitch setOn:YES];
} else {
[toggleSwitch setOn:NO];
}
toggleSwitch.tag = indexPath.row;
[toggleSwitch addTarget:self action:@selector(viewButtonPushed:) forControlEvents:UIControlEventValueChanged];
[toggleSwitch release];
return cell;
}
- (void)viewButtonPushed:(id)sender {
UIButton *button = (UIButton *)sender;
UITableViewCell *cell = button.superview; // adjust according to your UITableViewCell-subclass' view hierarchy
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
Category *cat = [allCategories objectAtIndex:indexPath.section];
Subject *sub = [cat.subjects objectAtIndex:indexPath.row];
selectedSubject = sub;
UISwitch* switchControl = sender;
NSLog(@"The switch is %@", switchControl.on ? @"ON" : @"OFF");
if(switchControl.on){
[sub setActive:1];
NSLog(@"%@ is being set to ACTIVE", selectedSubject.title);
}else{
[sub setActive:0];
NSLog(@"%@ is being set to INACTIVE", selectedSubject.title);
}
[sub setIsDirty:YES];
[cat.subjects replaceObjectAtIndex:indexPath.row withObject:sub];
[sub autorelease];
[cat autorelease];
}
這裏是我的didSelectRowAtIndexPath方法。我需要在此處提及toggleSwitch嗎?
- (void)tableAlert:(SBTableAlert *)tableAlert didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
Category *cat = [allCategories objectAtIndex:indexPath.section];
Subject *sub = [cat.subjects objectAtIndex:indexPath.row];
selectedSubject = sub;
NSLog(@"selectedSubject = %@", selectedSubject.title);
if (tableAlert.type == SBTableAlertTypeMultipleSelct) {
UITableViewCell *cell = [tableAlert.tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone)
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
else
[cell setAccessoryType:UITableViewCellAccessoryNone];
[tableAlert.tableView deselectRowAtIndexPath:indexPath animated:YES];
}
}
你知道你確實有,如果你在一個plist中或類似保存它寫入到磁盤的變化。僅僅因爲你改變了數組並不會改變文件本身。 – 2012-02-24 21:06:44
是的,我稍後再做。在進一步的調試中,我發現我的問題不在保存到數組中,而是在確定哪一行切換切換。 – jroyce 2012-02-24 21:37:23
在這裏看到我的答案一個整潔的方法做到這一點:http://stackoverflow.com/questions/9274494/how-to-know-the-uitableview-row-number/9274863#9274863 – jrturton 2012-02-24 21:59:44