iOS开发UItableview的常用属性方法的使用

有些属性和方法始终是记不清,只能记下来,方便查找

如果对你有帮助请支持,没有帮助请告诉我哪里需要改进!谢谢!

//  ViewController.m

//  TableViewAll

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor = [UIColor yellowColor];

    [self setupTableview];

}

- (void)setupTableview

{

    UITableView *myTblView = [[UITableView alloc]initWithFrame:CGRectMake(20, 20, [UIScreen mainScreen].bounds.size.width - 40, [UIScreen mainScreen].bounds.size.height - 40) style:UITableViewStylePlain];

    //UITableViewStylePlain,                  // 标准的表视图风格

    //UITableViewStyleGrouped                 // 分组的表视图风格

    myTblView.delegate = self;

    myTblView.dataSource = self;

    //tableview背景色

    myTblView.backgroundColor = [UIColor brownColor];

    

    //cell分割线只显示右边一半(设置cell分割线的长度)

    myTblView.separatorInset=UIEdgeInsetsMake(0, myTblView.frame.size.width/2, 0,0);

    

    //cell的分割线的颜色不是cell的属性,它属于tableView的separatorColor属性。

    [myTblView setSeparatorColor:[UIColor redColor]];

    

    //取消cell的分割线

    //    myTblView.separatorStyle = UITableViewCellSeparatorStyleNone;

    //UITableViewCellSeparatorStyleSingleLine  系统默认的

    //UITableViewCellSeparatorStyleSingleLineEtched  此分隔样式只支持当前分组样式表视图(用的比较少)

    

    //取消tableView右侧的滚动条

    myTblView.showsVerticalScrollIndicator = NO;

    

    //设置tableView的偏移量

    //    [myTblView setContentOffset:CGPointMake(0, 100) animated:YES];

    //当tableview数据较少时,动态隐藏tableView的底部线条

    myTblView.tableFooterView = [[UIView alloc]init];

    

    //隐藏tableView的footerView

    myTblView.sectionFooterHeight = 0;

    

    [self.view addSubview:myTblView];

}

//在tableView索引中显示搜索按钮

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

{

    NSMutableArray *arr = [[NSMutableArray alloc] initWithCapacity:0];

    [arr addObject:@"{search}"];//等价于[arr addObject:UITableViewIndexSearch];

    return arr;

}

//cell点击触发方法

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

{

    //tableView选中时反选

    //    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}

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

    //cell的数量

    return 10;

}

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

{

    //cell高度

    return 30;

}

//cell默认背景色的修改

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath  {

    cell.backgroundColor = [UIColor grayColor];

}

//设置cell

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

{

    static NSString *ident = @"shool";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ident];

    if (!cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ident];;

    }

    //cell选中时的背景色

    UIImageView *imageView = [[UIImageView alloc]init];

    imageView.backgroundColor = [UIColor yellowColor];

    cell.selectedBackgroundView = imageView;

    cell.backgroundColor = [UIColor whiteColor];

    /*

     cell.accessoryType =UITableViewCellAccessoryNone;//cell没有任何的样式

     cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;//cell的右边有一个小箭头,距离右边有十几像素;

     cell.accessoryType =UITableViewCellAccessoryDetailDisclosureButton;//cell右边有一个蓝色的圆形button;

     cell.accessoryType =UITableViewCellAccessoryCheckmark;//cell右边的形状是对号;

     cell.selectionStyle =UITableViewCellSelectionStyleNone;//cell选中状态的样式,为枚举类型

     */

    return cell;

}

/*

 获取多选cell的位置信息

 - (NSArray *)indexPathsForSelectedRows;

 

 tableview的编辑(属性类)

 设置是否是编辑状态(编辑状态下的cell左边会出现一个减号,点击右边会划出删除按钮)

 @property (nonatomic, getter=isEditing) BOOL editing;

 - (void)setEditing:(BOOL)editing animated:(BOOL)animated;

 

 设置cell是否可以被选中(默认为YES)

 @property (nonatomic) BOOL allowsSelection;

 

 设置cell编辑模式下是否可以被选中

 @property (nonatomic) BOOL allowsSelectionDuringEditing;

 

 设置是否支持多选

 @property (nonatomic) BOOL allowsMultipleSelection;

 

 设置编辑模式下是否支持多选

 @property (nonatomic) BOOL allowsMultipleSelectionDuringEditing;

 

 通过位置路径获取cell

 - (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;

 

 

 

 

 

 tableview插入分区

 - (void)insertSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

 删除分区

 - (void)deleteSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation;

 重载一个分区

 - (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation ;

 移动一个分区

 - (void)moveSection:(NSInteger)section toSection:(NSInteger)newSection;

 插入一些行

 - (void)insertRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

 删除一些行

 - (void)deleteRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

 重载一些行

 - (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation;

 移动某行

 - (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath;

 animation参数是一个枚举,枚举的动画类型如下

 UITableViewRowAnimationFade,//淡入淡出

 UITableViewRowAnimationRight,//从右滑入

 UITableViewRowAnimationLeft,//从左滑入

 UITableViewRowAnimationTop,//从上滑入

 UITableViewRowAnimationBottom,//从下滑入

 UITableViewRowAnimationNone,  //没有动画

 UITableViewRowAnimationMiddle,

 UITableViewRowAnimationAutomatic = 100  // 自动选择合适的动画

 

 */

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

posted @ 2017-08-17 18:13  屋巢  阅读(161)  评论(0编辑  收藏  举报