温馨提示×

温馨提示×

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

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

关于ui中的表视图

发布时间:2020-07-13 14:14:06 来源:网络 阅读:306 作者:hmymy 栏目:开发技术

#import "ViewController.h"


@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    self.data = [NSMutableArray arrayWithArray:[UIFont familyNames]];

    //设置表视图分割线风格没有分割线

    //_tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;

    //没有分割线

    //_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    //设置分割线颜色,默认为灰色

    _tableView.separatorColor = [UIColor orangeColor];

    //设置分割线的偏移量

    //_tableView.separatorInset = UIEdgeInsetsMake(0, -50, 0, -50);

    

    //设置表视图的头部视图

    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 160)];

    //创建按钮

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];

    btn.frame = CGRectMake(100, 50, 100, 100);

    [headerView addSubview:btn];

    

    [btn addTarget:self action:@selector(btn_click:) forControlEvents:UIControlEventTouchUpInside];

    headerView.backgroundColor = [UIColor yellowColor];

    _tableView.tableHeaderView = headerView;

    

    //设置表视图的尾部视图

    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 200)];

    footView.backgroundColor = [UIColor purpleColor];

    _tableView.tableFooterView = footView;

    

    //设置单元格的行高,默认为44,如果要动态的设置行高,需要在代理方法中实现

    //_tableView.rowHeight = 100;

    

}


- (void)btn_click:(UIButton *)btn{

   //点击按钮删除第一个元素

    [self.data removeObjectAtIndex:0];

    //刷新表视图,会重新执行数据源方法和代理方法

    //[self.tableView reloadData];

    

    //把第五个元素改为华文琥珀

    //构建IndexPath

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

    //取到单元格

    UITableViewCell *tvc = [self.tableView cellForRowAtIndexPath:indexPath];

    tvc.textLabel.text = @"华文琥珀";

    

//    //输出在屏幕上显示的单元格的内容

//    NSArray *cells = [self.tableView visibleCells];

//    for (UITableViewCell *cell in cells) {

//        NSLog(@"cell is %@",cell.textLabel.text);

//    }

//    

//    //输出在屏幕上显示的单元格的下标

//    NSArray *indexPaths = [self.tableView indexPathsForVisibleRows];

//    for (NSIndexPath *indexPath in indexPaths) {

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

//    }

    

//    //输出选中的单元格

//    NSArray *selecedRows = [self.tableView indexPathsForSelectedRows];

//    for (NSIndexPath *selectedRow in selecedRows) {

//        NSLog(@"%@",selectedRow);

//    }

    

    //滑动到指定位置,可配置动画

    NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:10 inSection:0];

    [self.tableView scrollToRowAtIndexPath:indexPath2 atScrollPosition:UITableViewScrollPositionTop animated:YES];


}


#pragma mark--UITableViewDataSource

//表视图的行数

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

    

    return self.data.count;

}


//单元格内容

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

    UITableViewCell *tvCell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

    NSString *str = self.data[indexPath.row];

    tvCell.textLabel.text = str;

    return tvCell;

}

//设置单元格的高度

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

    if (indexPath.row == 3) {

        return 80;

    }else if (indexPath.row == 5){

        return 100;

    }

    return 40;//无效,不会执行

    

}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end


向AI问一下细节

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

AI