#import "ViewController.h"
@interface ViewController ()
<
UITableViewDelegate,
UITableViewDataSource
>
@property(nonatomic,retain)NSArray *dataArray;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.dataArray = @[@"文强",@"小明",@"小强",@"额尔敦"];
UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
tableView.backgroundColor = [UIColor redColor];
//设置行高。
tableView.rowHeight = 200.0f;
//分割线的样式
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
//分割线的颜色
tableView.separatorColor = [UIColor cyanColor];
tableView.delegate = self;
tableView.dataSource = self;
[self.view addSubview:tableView];
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 300)];
headerView.backgroundColor = [UIColor greenColor];
tableView.tableHeaderView = headerView;
}
//设置有几个分组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 10;
}
//设置某一section中的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.dataArray.count;
}
//设置某一位置的cell,屏幕向上滑,每次出现一cell,这个方法走一次
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *name = self.dataArray[indexPath.row];
if ([@"额尔敦" isEqualToString : name ]) {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"eerdun"];
if (nil == cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"eerdun"];
}
return cell;
}
//当第一页的时候,重用池cell为空,
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//这里判断,如果重用池为空, 走判断里边的方法,创建一个cell,
if (nil == cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
cell.textLabel.text = name;
//下边的这句不可以写到判断里边,一旦写到判断里边,当重用池有东西以后,就不会再走这个方法,
//cell.textLabel.text = [NSString stringWithFormat:@"当前的行数:%ld",indexPath.row];
return cell;
}
//当点击某一个cell的时候回触发该方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//输出第几个分组第几行的内容
NSLog(@"section :%ld,row:%ld,content:%@",indexPath.section,indexPath.row,self.dataArray[indexPath.row]);
//取消选中的状态,点一下就么有了
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end
UITableView
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 如果此文帮助了您,请点击喜欢或评论,您的支持永远都是我前行的动力. 本人在关于viewController中的添加...
- 我在开发的时候遇到了两种情况,第一种 删除 一定要先把dataSource数组对应的indexPath.row删除...
- 点击这里查看http://blog.sina.com.cn/s/blog_7b9d64af0101aaoy.html