關於ui中的表視圖

#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 *indexPath1 = [NSIndexPath indexPathForRow:10 inSection:0];

    [self.tableView scrollToRowAtIndexPath:indexPath1 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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章