我试图从tableview单元格中删除子视图,我添加了像
subView.tag = 300;
[cell.contentView addSubView:subView];
使用此方法
[cell.contentView viewWithTag:300] performSelector:@selector(removeFromSuperView)];
但它不起作用
这是完整的代码
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
//Check if custom subvie already present.If yes, remove and return
if(![tableView isEditing])
{
[[cell.contentView viewWithTag:REORDER_CONTROL_TAG] performSelector:@selector(removeFromSuperview)];
return;
}
// Grip customization code goes in here...
UIView* reorderControl = [cell huntedSubviewWithClassName:@"UITableViewCellReorderControl"];
UIView* resizedGripView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(reorderControl.frame), CGRectGetMaxY(reorderControl.frame))];
resizedGripView.tag = REORDER_CONTROL_TAG;
[resizedGripView addSubview:reorderControl];
[cell.contentView addSubview:resizedGripView];
CGSize sizeDifference = CGSizeMake(resizedGripView.frame.size.width - reorderControl.frame.size.width, resizedGripView.frame.size.height - reorderControl.frame.size.height);
CGSize transformRatio = CGSizeMake(resizedGripView.frame.size.width / reorderControl.frame.size.width, resizedGripView.frame.size.height / reorderControl.frame.size.height);
// Original transform
CGAffineTransform transform = CGAffineTransformIdentity;
// Scale custom view so grip will fill entire cell
transform = CGAffineTransformScale(transform, transformRatio.width, transformRatio.height);
// Move custom view so the grip's top left aligns with the cell's top left
transform = CGAffineTransformTranslate(transform, -sizeDifference.width / 2.0, -sizeDifference.height / 2.0);
[resizedGripView setTransform:transform];
for(UIImageView* cellGrip in reorderControl.subviews)
{
if([cellGrip isKindOfClass:[UIImageView class]])
[cellGrip setImage:nil];
}
}
我正在学习本教程
http://b2cloud.com.au/how-to-guides/reordering-a-uitableviewcell-from-any-touch-point/
但问题是一旦添加了子视图,我就无法在单元格内的按钮上触摸事件。这就是为什么我试图删除它们
迭代内容视图子视图会发现没有给定标记的子视图。为什么我找不到它?