#import <UIKit/UIKit.h>
// 1 签订tableview的两个协议
@interface MainViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)NSMutableArray *arr;
@end
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// 创建表视图
UITableView *tavleView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
// 设置tavleView
// 宽度
tavleView.rowHeight = 100;
// 分割线样式
tavleView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
// 分割线颜色
tavleView.separatorColor = [UIColor cyanColor];
// 将tavleView的代理人(delegate)指定为当前的viewController
tavleView.dataSource = self;
tavleView.delegate = self;
[self.view addSubview:tavleView];
[tavleView release];
}
// 每个section(区)里面有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(@"显示多少行");
return [self.arr count];
}
// 每一行要显示什么样的cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *string = @"顾佳炜";
// 1.从重用池中获取一个cell
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
// 2.判断是否从重用池中取得cell
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:
UITableViewCellStyleSubtitle
reuseIdentifier:string]
autorelease];
NSLog(@"创建cell");
}
// 3.重新对cell赋值
// indexPath 有两个属性:section / row代表一个cell在tableView
cell.textLabel.text = [NSString stringWithFormat:@"%d, %d", indexPath.section, indexPath.row];
// 设置图片
cell.imageView.image =[UIImage imageNamed:@"1.jpg"];
// 副标题
// 根据indexpath的row 取得素组里的相应元素
cell.detailTextLabel.text = [self.arr objectAtIndex:indexPath.row];
return cell;
}
// tableView的delegate方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
SecondViewController *secondVC = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secondVC animated:YES];
}