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