- GitHub链接:
- https://github.com/CEWendel/SWTableViewCell
- IOS 8已经开放了相应API, 但是项目往往需要向前兼容,所以要采取自定义方法来实现
-
IOS8 使用下面的方法,可以轻松实现tableViewCell的多个自定义编辑按钮,但是为了兼容IOS7,只能自己实现
-
- (nullableNSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
-
附上使UIButton标题自动换行的实现方法:http://blog.sina.com.cn/s/blog_7f2b201701013b0o.html - 如果标题文字比较长,可以加入换行符,自动换行,另外要在原代码里面设置button title的方法中设置文字断行方式
-
就是这个方法:- (void)sw_addUtilityButtonWithColor:(UIColor *)color title:(NSString *)title
-
中添加:button.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
-

功能
右边的工具按钮
用户向左滑动可以调出工具按钮,在Table View Cell的右边显示。这种和iOS的系统Mail和Reminders类似。

左边的工具按钮
如果用户向右边滑动也可以调出工具按钮,在Table View Cell的左边显示。

特点
动态的工具按钮,大小可以调整。如果在一个cell中需要增加更多的按钮,那么其他的按钮会自动缩小提供空间。
按钮的名字和颜色可以自己调整。
在iOS6.1以上系统,包括iOS 7中测试通过。
用法
在tableView:cellForRowAtIndexPath:方法中创建一个SWTableView cell,使用NSMutableArray+SWUtilityButtons 类给它添加任意数量的工具按钮。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"Cell";
SWTableViewCell *cell = (SWTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
NSMutableArray *leftUtilityButtons = [NSMutableArray new];
NSMutableArray *rightUtilityButtons = [NSMutableArray new];
[leftUtilityButtons addUtilityButtonWithColor:[UIColor colorWithRed:0.07 green:0.75f blue:0.16f alpha:1.0] icon:[UIImage imageNamed:@"check.png"]];
[leftUtilityButtons addUtilityButtonWithColor:[UIColor colorWithRed:1.0f green:1.0f blue:0.35f alpha:1.0] icon:[UIImage imageNamed:@"clock.png"]];
[leftUtilityButtons addUtilityButtonWithColor:[UIColor colorWithRed:1.0f green:0.231f blue:0.188f alpha:1.0] icon:[UIImage imageNamed:@"cross.png"]];
[leftUtilityButtons addUtilityButtonWithColor:[UIColor colorWithRed:0.55f green:0.27f blue:0.07f alpha:1.0] icon:[UIImage imageNamed:@"list.png"]];
[rightUtilityButtons addUtilityButtonWithColor:[UIColor colorWithRed:0.78f green:0.78f blue:0.8f alpha:1.0] title:@"More"];
[rightUtilityButtons addUtilityButtonWithColor:[UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f] title:@"Delete"];
cell = [[SWTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier height:_tableView.rowHeight leftUtilityButtons:leftUtilityButtons rightUtilityButtons:rightUtilityButtons];
cell.delegate = self;
}
NSDate *dateObject = _testArray[indexPath.row];
cell.textLabel.text = [dateObject description];
cell.detailTextLabel.text = @"Some detail text";
return cell;
}
Delegate
开发者可以使用SWTableViewCellDelegate来了解用户点击了哪个按钮。有两种方法:
- (void)swippableTableViewCell:(SWTableViewCell *)cell didTriggerLeftUtilityButtonWithIndex:(NSInteger)index;
- (void)swippableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index;
index指示用户点击了哪个按钮。
例子
#pragma mark - SWTableViewDelegate
- (void)swippableTableViewCell:(SWTableViewCell *)cell didTriggerLeftUtilityButtonWithIndex:(NSInteger)index {
switch (index) {
case 0:
NSLog(@"check button was pressed");
break;
case 1:
NSLog(@"clock button was pressed");
break;
case 2:
NSLog(@"cross button was pressed");
break;
case 3:
NSLog(@"list button was pressed");
default:
break;
}
}
- (void)swippableTableViewCell:(SWTableViewCell *)cell didTriggerRightUtilityButtonWithIndex:(NSInteger)index {
switch (index) {
case 0:
NSLog(@"More button was pressed");
break;
case 1:
{
// Delete button was pressed
NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell];
[_testArray removeObjectAtIndex:cellIndexPath.row];
[self.tableView deleteRowsAtIndexPaths:@[cellIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
break;
}
default:
break;
}
}
提示
定制UITableViewCell内容
- 不要使用Storyboards来创建你的定制的UItableViewCell内容。只需要简单的在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中增加view到cell的contentview。
- 获仍然可以取cell object的view或者管理预定义的内容。所以说如果你想要改变cell的imageView 或者是backgroundView,SWTableViewCell,仍然可以按照你希望的变化进行改变。
- 不要在cell中使用accessory view,因为他们的层级处于contentView之上,如果cell消失了的话,他们不会随之消失 。
Seperator Insets
如果在iOS 7中你有一个左边的工具按钮,我推荐你改变TableView 的seperatorInsect,这样可以让seperator扩展到屏幕的长度。
tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
联系作者:
Chris Wendel: http://twitter.com/CEWendel