我试图在uitableviewcell内部切换开关时获取行号,但是我在enableAbsense方法下为indexpath获取null。知道哪里出错了吗?这是代码。提前谢谢。
在实现文件中: - (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 *)发件人 {
UITableViewCell *cell = (UITableViewCell *)sender.superview.superview;
NSIndexPath *indexpath =[DetailsViewClassTable indexPathForCell:cell];
NSLog(@"%ld",(long)indexpath.row);
}
头文件中的@property(强,非原子)IBOutlet UITableView * DetailsViewClassTable;
答案 0 :(得分:0)
-(void) enableAbsense:(UISwitch *)sender
{
CGPoint point = [sender.superview convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:point];
}