【发布时间】:2014-06-18 16:24:58
【问题描述】:
当在 uitableviewcell 内切换开关时,我试图获取行号,但是在 enableAbsense 方法下,索引路径为空。知道哪里出了问题吗?这是代码。提前致谢。
在实现文件中: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == DetailsViewClassTable) {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];// forIndexPath:indexPath];
ClassRecord * ClassObject;
ClassObject = [DetailsViewClassArray objectAtIndex:indexPath.row];
//add switch button switch
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
switchAbsense = [[UISwitch alloc] init];
CGSize switchSize = [switchAbsense sizeThatFits:CGSizeZero];
switchAbsense.frame = CGRectMake(cell.contentView.bounds.size.width - switchSize.width - 355.0f,
(cell.contentView.bounds.size.height - switchSize.height) / 5.0f,
switchSize.width,
switchSize.height);
switchAbsense.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
switchAbsense.tag = 101;
[switchAbsense addTarget:self action:@selector(enableAbsense:) forControlEvents:UIControlEventValueChanged];
[cell.contentView addSubview:switchAbsense];
}else{
switchAbsense=(UISwitch *)[cell.contentView viewWithTag:101];
}
// Configure the cell...
if ([ClassObject.absense isEqual:@"Yes"]){
switchAbsense.on=YES;
}else{
switchAbsense.on=NO;
}
if ([ClassObject.makeup isEqual:@"Yes"]){
switchMakeup.on=YES;
}else{
switchMakeup.on=NO;
}
//Accessory
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
}
-(void) enableAbsense:(UISwitch *)sender {
UITableViewCell *cell = (UITableViewCell *)sender.superview.superview;
NSIndexPath *indexpath =[DetailsViewClassTable indexPathForCell:cell];
NSLog(@"%ld",(long)indexpath.row);
}
在头文件中 @property (strong, nonatomic) IBOutlet UITableView *DetailsViewClassTable;
【问题讨论】:
标签: uitableview uiswitch