对于iOS开发的同学们来说,UITableView应该是熟悉的不能再熟悉的控件了,对于表视图,应该都用烂了,我们都知道UITableViewCell里面的accessoryType有各种各样样式,可是我们如何修改这些系统原生的样式呢,有同学说我们可以使用accessoryView进行自定义啊,是的,这样可以满足部分需求,如果现在有一个需求就是更改UITableViewCell的ReorderControl(拖动样式),系统默认是灰色的三个横线,这个时候我们又应该如何做呢?
这就需要我们了解UITableViewCell的层级结构关系
在正常情况下,UITableViewCell的结构图
在编辑情况下(删除,插入,拖动)UITableViewCell的结构图
我们可以看到reordering control就是我们要修改的部分,那么如何进行修改呢,下面演示部分代码
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
for (UIView *view in tableView.subviews) {
if([[[view class] description] isEqualToString:@"UITableViewWrapperView"]){
for (UIView *viewone in view.subviews) {
if([[[viewone class] description] isEqualToString:@"UITableViewCell"]) {
for (UIView *viewtwo in viewone.subviews) {
if([[[viewtwo class] description] isEqualToString:@"UITableViewCellReorderControl"]) {
// 为这个cell添加一个新的大小一样的subView
UIView *movedReorderControl = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(viewtwo.frame), CGRectGetMaxY(viewtwo.frame))];
[movedReorderControl addSubview:viewtwo];
// 将这个新的子类添加到cell上
[viewone addSubview:movedReorderControl];
// 平移量为整个cell的宽度减去ReorderControl的宽度
CGSize moveLeft = CGSizeMake(movedReorderControl.frame.size.width - viewtwo.frame.size.width, movedReorderControl.frame.size.height - viewtwo.frame.size.height);
CGAffineTransform transform = CGAffineTransformIdentity;
transform = CGAffineTransformTranslate(transform, -moveLeft.width, -moveLeft.height);
// 执行平移操作
[movedReorderControl setTransform:transform];
// 添加图片
for (UIImageView *imageV in viewtwo.subviews) {
imageV.image = [UIImage imageNamed:@"ItyneraryCanMove"];
}
}
}
}
}
}
}
}
github完整地址https://github.com/likelin/ReorderControlTest.git