iOS Table View Cell 自定义左滑按钮及事件(系统自带方法)

iOS Table View Cell 自定义左滑按钮及事件(系统自带方法),第1张

在项目需求中,经常会有对 TableView Cell 左滑事件及按钮的自定义设置需求

在iOS 11.0之前,我们可以看到 Cell 的左滑界面 UITableViewCellDeleteConfirmationView 布局是在于 Cell 之中的,并且只有左滑事件之后才会添加到 Cell 中,在左滑界面中存在一个 UIButton 的子视图,这便是我们需要自定义的视图

在 tableView 的 delegate 方法中自带有 - (NSArray *)tableView:(UITableView*)tableView editActionsForRowAtIndexPath:(NSIndexPath*)indexPath 可自定义事件

至此iOS 11.0之前的版本的左滑自定义便已经可以了(当然,也可以自定义左滑时多个事件,原理也是一样的)

在iOS 11.0之后的版本,左滑布局发生了改变

iOS 11.0之后的当然能用之前的方法了,而苹果在11.0之后其实也新增了另外一个新的方法 - (UISwipeActionsConfiguration *)tableView:(UITableView *)tableView trailingSwipeActionsConfigurationForRowAtIndexPath:(NSIndexPath *)indexPath ,只在iOS 11.0之后的系统才有效,其实目前区别上也仅仅是action多了一个image属性,然而此image属性也不能显示原图,而是会被自动渲染。

GItHubDemo

之前许多小伙伴可能会直接把按钮添加到tableview的cell上,类似:[self addsubviews:btn]

iOS14系统下会造成这类按钮无法点击。

原因是:cell中contentview改为了懒加载,如果没有.contentview的方法,按钮会被contentview覆盖。也是就是按钮会比contentview提前创建并添加到cell上。

所以以后有类似习惯的小伙伴尽量把控件添加到contentview上,而不是直接添加到cell上。

1.首先创建一个Single View Project,命名为UITableViewControllerTest。打开ViewController.xib并拖入一个UITableView,选中该视图,并点击Connections inspector,分别将delegate和dataSource连线到File's owner

2.在AppDelegate.h中添加属性

@property (strong, nonatomic) IBOutlet UITabBarController * rootViewController

并在.m文件中添加合成器

@synthesize rootViewController

然后在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中添加该TableView为subView

3.在ViewController.h文件中添加协议UITableViewDelegate,UITableViewDataSource,然后在.m文件中实现协议中的方法

#pragma mark -

#pragma mark Table View Data Source Methods

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return [self.listDatas count]

}

上面的方法是返回Table中的行数

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString * SimpleTableViewIdentifier =@"SimpleTableViewIdentifier"

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableViewIdentifier]

if (cell == nil) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableViewIdentifier]

}

UIImage * img = [UIImage imageNamed:@"singleicon"]

cell.imageView.image = img

NSUInteger row = [indexPath row]

cell.textLabel.text = [listDatas objectAtIndex:row]

return cell

}

上面的方法是构造每一个cell,也就是每一行,SimpleTableViewIdentifier是标记,dequeueReusableCellWithIdentifier方法是当有可重用的cell时,根据标记直接取,而不用重新生成,可以提高效率。

NSIndexPath封装了有关行和分区的信息

UIImage * img = [UIImage imageNamed:@"singleicon"]

cell.imageView.image = img

NSUInteger row = [indexPath row]

cell.textLabel.text = [listDatas objectAtIndex:row]

这段代码分别设置了行的图片和文字,另外,通过accessoryType属性可以设置行的附属属性,有几种样式可以提供选择。也可以自定义附属视图:cell.accessoryView = [[myView alloc] init]

4.在viewDidLoad方法中添加NSArray后,填入数据,然后run,可以看打运行结果

在这里我使用了TabBarController来承载三种类型的tableview,分别是Plain、Group以及Custom自定义的

5.下面看第二种样式,分组的形式

使用分组的话还要添加一个方法,返回一共有多少组

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

另外可以设置分组标题

//返回组的名称

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

if ([keys count] == 0) {

returnnil

}

NSString * key = [keys objectAtIndex:section]

return key

}

//设置行的高度

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

return60

}

#pragma mark Table Delegate Methods

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

NSUInteger row = [indexPath row]

NSString * message = [[NSString alloc]initWithFormat:@"You selected is %@!",[listDatas objectAtIndex:row]]

UIAlertView * alert = [[UIAlertView alloc]initWithTitle:@"Information" message:message delegate:nilcancelButtonTitle:@"Confirm" otherButtonTitles:nil,nil]

[alert show]

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11672254.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存