UITableView 表视图
来源:互联网 发布:旅游软件有哪些 编辑:程序博客网 时间:2024/05/16 16:56
UITableView 继承自UIScrollView, 可以进行 分区(section),行(row), frame 决定tableView显示的位置,边框,
row表示tableView中某一行的位置,数量决定contentSize, tableView根据行数自行设置, 每一行的位置都会放置一个UITableViewCell负责显示文本。
cell 有重用机制,cell作为显示数据的。这个重用机制就是说,如果有1000个row,就需要1000个cell,这样太浪费, 可以吧显示到屏幕上cell数量j
进行重用,随着滑动,每次都把数据显示到这几个cell中。
UITableView 属性
Style, 分割线样式,分割线颜色, 行高, 控制代理, 数据代理。
- (void)viewDidLoad {
[superviewDidLoad];
[self.viewsetBackgroundColor:[UIColorwhiteColor]];
UITableView *tView = [[UITableViewalloc] initWithFrame:CGRectMake(0,0, 390,self.view.frame.size.height)style:UITableViewStylePlain];
//设置行高
[tViewsetRowHeight:80.0];
//设置分割线颜色
[tView setSeparatorColor:[UIColorblueColor]];
//设置cell的样式
[tView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLineEtched];
[self.viewaddSubview:tView];
// Do any additional setup after loading the view, typically from a nib.
}
UITableView有两个协议, DataSource, Delegate。
UITableViewDataSource
有三个方法
一个可选的
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
两个必须实现的
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//返回多少个section, 如果返回1, 就只有一个,这里返回四个
return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//每个section 返回多少行
if (section == 0) {
return 2;
}
if (section == 1) {
return 2;
}
if(section == 2) {
return 3;
}else {
return 3;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//cell标识
static NSString *cellIdentifier =@"cell";
//查备用队列
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
//队列里没有,就从新创建一个cell
if (!cell) {
cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIdentifier];
}
// 在每一个section上显示数据,
cell.textLabel.text = [NSStringstringWithFormat:@"section = %d, row = %d", indexPath.section, indexPath.row];
return cell;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section ==0) {
return 40;
}
if (indexPath.section ==1) {
if (indexPath.row ==0) {
return 40;
}
return 60;
}
return 50;
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [NSArrayarrayWithObjects:@"1",@"2", @"3",@"4", nil];
}
//设置section 头
- (NSString *) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [NSStringstringWithFormat:@"%ld", section+1];
//return @"HEAD TEST";
}
//设置section尾;
- (NSString *) tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
return@"FOOTER TEST";
}
- UITableView表视图控件
- swift UITableView 表视图
- UITableView 表视图
- UITableView 表视图
- UI UITableView表视图
- UITableView 表视图
- UITableView表视图
- UITableView 表视图
- UITableView(表视图)
- UITableView:表视图
- [TwistedFate]UITableView表视图
- 11 UITableView表视图
- UITableView(表视图)
- UITableView表视图简单使用
- UITableView表视图的编辑
- iOS UITableView表视图(1)
- ios UITableView表视图(2)
- 第九讲:UITableView表视图
- 四级作文的一些句子模板
- J2EE 中的几种常用模式?及对设计模式的一些看 法
- Warning: Build-tool 17.0.0 is missing AAPT at F:\adt-bundle-windows-x86\sdk\
- 理解arcgis server 10.1 服务发布与数据管理
- JSP注释和声明
- UITableView 表视图
- 第十四周 项目1 - 小玩文件(1)填空
- CSU1632: Repeated Substrings(后缀数组)
- window.open详解
- RedHat Enterprise Linux 5 NFS服务器
- windows oracle EM的安装配置
- 死锁的原因、条件和避免死锁的方法
- 错误2error LNK2019: 无法解析的外部符号 "public: __thiscall obj3dModel::~obj3dModel(void)" (??1obj3dModel@@QAE
- Snprintf Strncpy Memcpy Strlcpy各自的特点