您的位置:首页 > 产品设计 > UI/UE

UITableView 的简单介绍

2016-01-09 00:27 471 查看
UItableView的使用主要是其协议方法,其协议有两个<UITableViewDelegate,UITableViewDataSource>。

设置 UITableView的委托(两个委托属性);
tableView.dataSource =self;
tableView.delegate =self;
interface DemoViewController () <UITableViewDataSource, UITableViewDelegate>
@end

遵循协议;
<UITableViewDataSource, UITableViewDelegate>

@implementation DemoViewController
- (void)viewDidLoad {

    [super viewDidLoad];

    UITableView *tableView = [[UITableView alloc] initWithFrame:reck style:UITableViewStylePlain];

    tableView.dataSource = self;

    tableView.delegate = self;

    [self.view addSubview:tableView];

}
#pragma mark - Table view协议方法
协议里的两个必须实现的方法:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSecti

1,初始化方法:
UITableView *tableView =[UITableView alloc]initWithFrame:(CGRect);
// style参数指定样式,有两种:普通样式
Plain(默认值)、分组样式
Grouped
UITableView *tableView =[UITableView alloc]initWithFrame:(CGRect) style:(UITableViewStyle)

//改变换行线颜色。
tableView.separatorColor = [UIColorpurpleColor];
 //判断tableView是否能被编辑。
   BOOL isEditing = tableView.isEditing;
   //设置tableView的能否被编辑
    [tableView   setEditing:isEditinganimated:YES];
//⭐️重新加载cell的内容。
    [tableViewreloadRowsAtIndexPaths:[NSArrayarrayWithObjects:indexPath,nil]withRowAnimation:UITableViewRowAnimationNone];

//同一个UIView的赋值给tv.tableHeaderView和tv.tableFooterView是,只会显示tv.tableHeaderView。

    tv.tableHeaderView =UIView *
iv;
   
    tv.tableFooterView =UIView *
iv;
    //分隔线。
    tv.separatorStyle =UITableViewCellSeparatorStyleSingleLine;

   
    tv.separatorColor = [UIColorgreenColor];
   //tableViewCellde附件种类
    tableViewCell.accessoryType

   

//右侧添加一个索引表。
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;
//设置 UITableView中,有多少个
Section (组)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView ;
//设置 UITableView中,每个
Section中有多少个 Cell (确定矿场一个区域能停多少矿车)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
/* ========== Cell设置
Cell的样式 ========== */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath ;
// 注册cell,与下面的复用机制联用。
[tableView registerClass:[MyTableViewCell
 class]forCellReuseIdentifier:@"MyTableViewCell"];
        
    
 //(Cell的复用机制),先去可复用
Cell队列里面找一下,看看是否有可以复用的
Cell//
 UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"Cell"forIndexPath:indexPath];
/* ========== Cell点击事件
========== */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath;
//设置 Cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath
*)indexPath ;
//设置section的 header、footer的标题
(NSString *)
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
;
- (NSString *)tableView:(UITableView
*)tableView titleForFooterInSection:(NSInteger)section;
//自定义section的
header、footer (UIView *)
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section 
- (UIView *)tableView:(UITableView
*)tableView viewForHeaderInSection:(NSInteger)section 
//设置 header、footer高度
(CGFloat)
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- (CGFloat)tableView:(UITableView
*)tableView heightForFooterInSection:(NSInteger)section 
//设置某行 Cell是否可以被编辑
(是否可以向左滑动,拖出编辑按钮)
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath
*)indexPath ;
//设置所编辑cell"删除"按钮的
Title
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath
*)indexPath ;
//当编辑按钮(默认叫"删除")被按下去的时候,执行的方法(类似
Button 点击事件)
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath;

//将要选择某一个cell时。

-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath
*)indexPath{

    if (indexPath.row ==0)
{
      
       return
nil;  
//设置不让选中每组的第一个。
    }

    return indexPath;

}

返回当前所选的cell。

NSIndexPath *ip = [NSIndexPath indexPathForRow:row inSection:section];

[tableView selectRowAtIndexPath:ip animated:YES scrollPosition:UITableViewScrollPositionNone];

//一个section刷新

NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:2];
[tableview reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];

//一个cell刷新

NSIndexPath *indexPath=[NSIndexPath indexPathForRow:3 inSection:0];

[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];

   
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iOS uitableview