您的位置:首页 > 其它

tableView 详细使用方法

2015-12-10 21:52 429 查看
//  UITableViewStylePlain  按行布局样式
UITableView *myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 375, 667 - 64) style:UITableViewStylePlain];

myTableView.backgroundColor = [UIColor whiteColor];
//     签两个代理人
myTableView.delegate = self;
myTableView.dataSource = self;

//去掉 cell 上的线
myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//多余的cell不显示
self.myTableView.tableFooterView = [UIView new];

//取消cell点击
self.myTableView.allowsSelection = NO;

//******************
//    设置 tableView 的表头视图  可以放轮播图
UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 375, 200)];
headerView.backgroundColor = [UIColor grayColor];
//    在这加上来就可以了  设置 tableView 的表头视图
myTableView.tableHeaderView = headerView;
[headerView release];

[self.view addSubview:myTableView];
[myTableView release];

//     刷新整个 TableView
[myTableView reloadData];

//     刷新 某个 cell    数组里是 要刷新的index 数组
//    myTableView reloadRowsAtIndexPaths:<#(NSArray *)#> withRowAnimation:<#(UITableViewRowAnimation)#>

//     设置 背景图片
// UIImageView *imageView = [[UIImageView alloc]initWithFrame:self.view.frame];
//    imageView.image = [UIImage imageNamed:@"back.png"];
//  背景图片
//    myTableView.backgroundView = imageView;
//    [imageView release];

}

// cell 的行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}

//  选中 cell的 触发方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    NSLog(@" 我被选中了 %ld, %ld", indexPath.section, indexPath.row);

//    选中时有置灰效果,离开时 选中效果 消失
[tableView deselectRowAtIndexPath:indexPath animated:YES];

//取消cell的点击事件
if (2 == indexPath.row) {
//取消某行cell点击事件
Cell.selectionStyle = UITableViewCellSelectionStyleNone;
return nightCell;
}

//    自定义 一个 NSIndexPath   用于滚到这行
NSIndexPath *myIndex = [NSIndexPath indexPathForRow:2 inSection:2];
//     滚动到某一行 cell
[tableView scrollToRowAtIndexPath:myIndex atScrollPosition:UITableViewScrollPositionBottom animated:YES];

//     跳转 界面
//    CellViewController *cellView = [[CellViewController alloc]init];
//    NSString *str = [NSString stringWithFormat:@"%ld , %ld", indexPath.section, indexPath.row];
//    cellView.string =str;
//    [self.navigationController pushViewController:cellView animated:YES];
//    [cellView release];

}

//   选中时不触发,当选中下一个时触发
//- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
//    NSLog(@" 我是上一个选中时触发的 %ld, %ld", indexPath.section, indexPath.row);
//}

//设置 section 表头标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
if (0 == section) {
return @"AA";
}
if (1 == section) {
return @"BB";
}
if (2 == section) {
return @"CC";
} else {
return @"DD";
}
}

//   索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [NSArray arrayWithObjects:@"AA", @"BB", @"CC", @"DD", nil];
}

// 设置 Section 数 ,即:区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 4;
}
//***************************
//   两个必须实现的方法
//  设置每个 section 的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (0 == section) {
return 2;   // 0 的区设置 2行
}else if(1 == section) {
return 3;   // 1 的区设置 3行
//取消某行cell点击事件
nightCell.selectionStyle = UITableViewCellSelectionStyleNone;
}else {
return 20;   // 2 的行 设置 4行
}
}

//  设置 行的内容    内部犹如一个大循环
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

//    1. 先从从用池取
//       queue:队列  Reusable:重用    Identifier:标示符
//     标示符 是区别不同 cell 唯一的标记
static NSString *cellIdentifier = @"cell";
//    给一个cell做标记
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//     cell 为空的时候 ,需要创建 ,即:第一次创建,之后就不用创建了
if (nil == cell) {
//         初始化一个          UITableViewCellStyleDefault 是不显示 副标题的
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
//         cell 可以设置为透明
cell.backgroundColor = [UIColor clearColor];
//       取消 cell的选中效果
//        cell.selectionStyle = UITableViewCellSelectionStyleNone;  //这样用户体验不好 我们用下面这个:
//    选中时有置灰效果,离开时 选中效果 消失
//       这句话在选中cell时触发的方法里设置    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}
//        0 区
if (0 == indexPath.section) {

cell.textLabel.text = @"芦珊";  // 主标签
NSString *str = [NSString stringWithFormat:@"(%ld , %ld)", indexPath.section, indexPath.row];
cell.detailTextLabel.text = str; //副标题
cell.imageView.image = [UIImage imageNamed:@"qq.png"];  // 设置图片
}
//      1 区
if(1 == indexPath.section) {
cell.textLabel.text = @"涛哥";
NSString *str = [NSString stringWithFormat:@"(%ld , %ld)", indexPath.section, indexPath.row];
cell.detailTextLabel.text = str; // 设置 副标题
}
//    2 区
if(2 == indexPath.section) {
cell.textLabel.text = @"大哥大哥";
NSString *str = [NSString stringWithFormat:@"(%ld , %ld)", indexPath.section, indexPath.row];
cell.detailTextLabel.text = str; //        设置 副标题
}

if (indexPath.section == 1 && indexPath.row == 2) {
cell.textLabel.text = @"大水杯";
}

return cell;
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

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