这是一个小的知识点,做出来的效果如图所示:
虽然很简单的页面,可是初学的时候还是很纠结的没有做出来,在这里写出他的程序
第一步:
第二步:
在MangoViewController.h中遵循<UITableViewDataSource>协议
第三步:
#import "MangoViewController.h"
@interface MangoViewController ()
@property(nonatomic,retain) UITableView *tableView;
@end
@implementation MangoViewController
- (void)dealloc{
[_tableView release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor cyanColor];
//设置导航栏标题
self.navigationItem.title = @"联系人";
//创建一个UITableView
self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
//分割线颜色
self.tableView.separatorColor = [UIColor redColor];
//分割线样式
self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
//背景颜色
self.tableView.backgroundColor = [UIColor yellowColor];
//设置tableView数据源代理
self.tableView.dataSource = self;
//添加到当前视图
[self.view addSubview:self.tableView];
//释放
[self.tableView release];
}
//分区中有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 101;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//重用机制实现原理
static NSString *cellIdentifier = @"CycleCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];
cell.imageView.image = [UIImage imageNamed:@"3.jpg"];
return cell;
}
//默认分区1个
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
总结:
1.cell.imageView.image = [UIImage imageNamed:@"3.jpg"];是添加图片
2.cell.textLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row];实现0~100