6
我有TableCellViewController用於管理UITableView中的單元格。每個單元都有一個標籤na UISwitch(dictTypeSwitch)。想要分配方法來切換事件,以便我可以保存按鈕的狀態。添加自定義操作:@selector到TableViewCell上的UISwitch
到目前爲止,我已經做到了這一點:
分配的setState函數對象:
[cell.dictTypeSwitch addTarget:self action:@selector(setState:) forControlEvents:UIControlEventTouchUpInside];
功能用於處理事件:
- (void)setState:(id)sender {
BOOL state = [sender isOn];
NSString *rez = state == YES ? @"YES" : @"NO";
NSLog(rez);
}
從寄件人我得到UISwitch對象從中我可以得到狀態。
到目前爲止一切正常。
但是,如果我想保存UISwitch的狀態,我必須爲此單元格獲取rowIndex。 我該如何做到這一點?
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;
}
感謝
感謝肯尼... – 2010-07-09 11:28:38