温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

iOS tableView的用法

发布时间:2020-06-17 19:21:33 来源:网络 阅读:367 作者:大头狼小鬼 栏目:移动开发

- (void)viewDidLoad

{

    [super viewDidLoad];


    UITableView *myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, Screen_width, Screen_height)style:UITableViewStylePlain];

//    UITableViewStyleGrouped分组

    myTableView.dataSource = self;

    myTableView.delegate = self;

    [self cancelCell:myTableView];

    [self.view addSubview:myTableView];

       // Do any additional setup after loading the view,;typically from a nib.

}


#pragma mark-once 行数

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

{

    return 3;

}


#pragma mark-once 行内容

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

{

   static NSString *cellID = @"cellID";

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:cellID];

    if(cell==nil)

    {

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

    }

    //UITableViewCellStyle          是个枚举

    //UITableViewCellStyleDefault;  默认的

    //UITableViewCellStyleSubtitle;

    //UITableViewCellStyleValue1;

    //UITableViewCellStyleValue2;

    cell.textLabel.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row];

    

    //给右边加个标识符的

    cell.accessoryType  =UITableViewCellAccessoryDisclosureIndicator;

    //选中的颜色

    cell.selectionStyle = UITableViewCellSelectionStyleBlue;

    

    return cell;

}


#pragma mark-once 几组

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

{

    return 2;

}


#pragma mark-once头标题

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

{

  return @"第一个标题";

}


#pragma mark-once未标题

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

{

    return @"最后一个标题";

 

}


#pragma mark-once标识符accessoryButtonTappedForRow方法

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

{


}


#pragma mark-once类似电话那种最右边的 abcde-z

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

{

    NSArray *rightArry = @[@"A",@"B",@"c",@"d",@"e",@"f",@"g"];

    return rightArry;

}

#pragma mark-once行高

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

{

    return 50;

}


#pragma mark-once头标题的高度

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section

{

    return 50;

}


#pragma mark-once未标题的高度

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section

{

    return 50;

}


#pragma mark-once行高

-(void)cancelCell:(UITableView *)tableView

{

    UIView *view = [UIView new];

    view.backgroundColor = [UIColor clearColor];

    [tableView setTableFooterView:view];

    view.frame = CGRectMake(0, 0, tableView.frame.size.width, 20);

}


#pragma mark-once 选中的方法

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

{

    NSLog(@"%ld",(NSInteger)indexPath.row);

}


#pragma mark-once编辑的方法

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{

    return YES;

}


#pragma mark-once是否允许移动

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

    return YES;

}


#pragma mark-once 编辑的方法

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath

{

}


#pragma mark-once 移动的方法

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath

{


}

#pragma mark-once 分割线

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

    

    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {

        [tableView setSeparatorInset:UIEdgeInsetsZero];

    }

    

    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {

        [tableView setLayoutMargins:UIEdgeInsetsZero];

    }

    

    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {

        [cell setLayoutMargins:UIEdgeInsetsZero];

    }

}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI