iOS UITableView 便捷编写
2015-12-30 13:58
495 查看
- (void)_prepareUI
{
CGRect rect = CGRectMake(MainSreenOrigin_X,MainSreenOrigin_Y,MainScreenSize_W,MainScreenSize_H);
_myTableView = [[UITableView alloc] initWithFrame:rect style:UITableViewStylePlain];
_myTableView.delegate = self;
_myTableView.dataSource = self;
_myTableView.scrollEnabled= YES;
_myTableView.backgroundColor=[UIColor clearColor];
[self.view addSubview:_myTableView];
// 改变 tableView的背景色
UIView * backColorView = [[UIView alloc] init];
backColorView.backgroundColor = PView_BGColor;
_myTableView.backgroundView = backColorView;
// 去掉多余的分割线
UIView * footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MainScreenSize_W, 0)];
footerView.backgroundColor = [UIColor clearColor];
_myTableView.tableFooterView = footerView;
[self.view addSubview:_myTableView];
}
#pragma mark ---
#pragma mark --- UITableViewDelegate ---
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] ;
}
cell.selectionStyle=UITableViewCellSelectionStyleNone;
// [cell addCellData:nil];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
自定义cell里面的方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.selectionStyle=UITableViewCellSelectionStyleNone;
}
return self;
}
/*删除用到的函数*/
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle ==UITableViewCellEditingStyleDelete){
[_classList removeObjectAtIndex:indexPath.row]; //删除数组里的数据
[tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationAutomatic]; //删除对应数据的cell
}
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
/*改变删除按钮的title*/
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"移除";
}
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
//可添加多个
UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"移除"handler:^(UITableViewRowAction*action,NSIndexPath *indexPath) {
[_classList removeObjectAtIndex:indexPath.row]; //删除数组里的数据
[tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationAutomatic]; //删除对应数据的cell
}];
deleteRowAction.backgroundColor = PView_OrangeColor;
return @[deleteRowAction];
}
{
CGRect rect = CGRectMake(MainSreenOrigin_X,MainSreenOrigin_Y,MainScreenSize_W,MainScreenSize_H);
_myTableView = [[UITableView alloc] initWithFrame:rect style:UITableViewStylePlain];
_myTableView.delegate = self;
_myTableView.dataSource = self;
_myTableView.scrollEnabled= YES;
_myTableView.backgroundColor=[UIColor clearColor];
[self.view addSubview:_myTableView];
// 改变 tableView的背景色
UIView * backColorView = [[UIView alloc] init];
backColorView.backgroundColor = PView_BGColor;
_myTableView.backgroundView = backColorView;
// 去掉多余的分割线
UIView * footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MainScreenSize_W, 0)];
footerView.backgroundColor = [UIColor clearColor];
_myTableView.tableFooterView = footerView;
[self.view addSubview:_myTableView];
}
#pragma mark ---
#pragma mark --- UITableViewDelegate ---
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] ;
}
cell.selectionStyle=UITableViewCellSelectionStyleNone;
// [cell addCellData:nil];
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
自定义cell里面的方法
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.selectionStyle=UITableViewCellSelectionStyleNone;
}
return self;
}
/*删除用到的函数*/
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle ==UITableViewCellEditingStyleDelete){
[_classList removeObjectAtIndex:indexPath.row]; //删除数组里的数据
[tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationAutomatic]; //删除对应数据的cell
}
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
/*改变删除按钮的title*/
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
return @"移除";
}
- (NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
//可添加多个
UITableViewRowAction *deleteRowAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"移除"handler:^(UITableViewRowAction*action,NSIndexPath *indexPath) {
[_classList removeObjectAtIndex:indexPath.row]; //删除数组里的数据
[tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationAutomatic]; //删除对应数据的cell
}];
deleteRowAction.backgroundColor = PView_OrangeColor;
return @[deleteRowAction];
}
相关文章推荐
- Builder模式在Java中的应用
- UIProgressView
- [LeetCode]Distinct Subsequences
- 建造者模式【Builder Pattern
- UIViewController和UIView的关系
- UISegmentedControl详解
- UITextView富文本
- iOS UI-AlertView(警示框)和ActionSheet(选择框、操作表单)
- [ios]纯代码实现UITableViewCell的自定义扩展
- EasyUI form ajax submit到MVC后,在IE下提示下载内容的解决办法
- iOS UI-三种简单的动画设置
- iOS UI-IOS开发中Xcode的一些使用技巧
- UITableViewCell自适应高度
- UE中使用正则表达式的一些技巧
- (UIAlertView)封装的弹出框和风火轮
- Mac Error Create Android Project - “Errors running builder 'Android Resource Manager' on project”
- UIView的圆角
- HDU 5538.House Building【求表面积】【2015ACM/ICPC亚洲区长春站】【12月30】
- java自带uuid生成
- iOS8 Self Sizing UITableView Cells iOS8Tableview Cells 自适应高度