【发布时间】:2016-11-07 04:39:59
【问题描述】:
嗨,在我的应用程序中,我们要求表格视图单元格应该是可编辑的,这意味着可以更改单元格的顺序。为此,我正在使用重新排序控制。随着这个重新排序控制应该覆盖整个单元格,而不仅仅是右端。为此,我正在对显示单元格方法进行以下更改。
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
for (UIView * view in cell.subviews)
{
if ([NSStringFromClass([view class]) rangeOfString: @"Reorder"].location != NSNotFound)
{
// [view setBackgroundColor:[UIColor redColor]];
UIView* resizedGripView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(view.frame), CGRectGetMaxY(view.frame))];
[resizedGripView setBackgroundColor:[UIColor clearColor]];
[resizedGripView addSubview:view];
resizedGripView.backgroundColor=[UIColor clearColor];
resizedGripView.userInteractionEnabled=YES;
[cell.contentView addSubview:resizedGripView];
CGSize sizeDifference = CGSizeMake(resizedGripView.frame.size.width - view.frame.size.width, resizedGripView.frame.size.height - view.frame.size.height);
CGSize transformRatio = CGSizeMake(resizedGripView.frame.size.width / view.frame.size.width, resizedGripView.frame.size.height / view.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 view.subviews)
{
if([cellGrip isKindOfClass:[UIImageView class]])
[cellGrip setImage:nil];
}
}
}
}
我使用的单元格是自定义单元格,单元格如下所示。
在我对显示单元方法进行了上述更改之后。我可以从任何地方重新排序单元格上的单元格,而不仅仅是从右边缘。正如我们所见,单元格上的删除按钮在此更改后不起作用,因为即使在按钮上也有一个视图被添加到单元格上。单元上可用的任何组件都应该接受事件。请帮助我。
【问题讨论】:
标签: ios objective-c uitableview custom-cell