UITableView基本使用方法1
2015-09-23 21:26
423 查看
1.
首先,Controller需要实现两个 delegate ,分别是 UITableViewDelegate 和 UITableViewDataSource
2.然后 UITableView对象的 delegate要设置为 self。
3. 然后就可以实现这些delegate的一些方法拉。
(1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
这个方法返回tableview 有多少个section
[cpp] viewplaincopy
//返回有多少个Sections
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
(2)- (NSInteger)tableView:(UITableView *)tablenumberOfRowsInSection:(NSInteger)section;这个方法返回
对应的section有多少个元素,也就是多少行
[cpp] viewplaincopy
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
(3)-(CGFloat)tableView:(UITableView *)tableViewheightForRowAtIndexPath:(NSIndexPath*)indexPath;
这个方法返回指定的 row 的高度。
- (CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section; 这个方法返回指定的
section的header view的高度。 - (CGFloat)tableView:(UITableView *)tableViewheightForFooterInSection:(NSInteger)section; 这个方法返回指定的section的footer
view 的高度。
(4)-(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath;返回指定的row的cell。这个地方是比较关键的地方,一般在这个地方来定制各种个性化的cell元素。这里只是使用最简单最基本 的cell
类型。其中有一个主标题cell.textLabel 还有一个副标题cell.detailTextLabel, 还有一个image在最前头叫 cell.imageView. 还可以设置右边的图标,通过cell.accessoryType 可以设置是饱满的向右的蓝色箭头,还是单薄的向右箭头, 还是勾勾标记。
[cpp] viewplaincopy
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * showUserInfoCellIdentifier = @"ShowUserInfoCell";
UITableViewCell * cell = [tableView_ dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier];
if (cell == nil)
{
// Create a cell to display an ingredient.
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:showUserInfoCellIdentifier]
autorelease];
}
// Configure the cell.
cell.textLabel.text=@"签名";
cell.detailTextLabel.text = [NSString stringWithCString:userInfo.user_signature.c_str() encoding:NSUTF8StringEncoding];
}
(5)-(CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section 返回指定的
section 的header的高度
[cpp] viewplaincopy
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section ==0)
return 80.0f;
else
return 30.0f;
}
(6)- (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section
返回指定的section 的 header 的title,如果这个section header 有返回view,那么title就不起作用了
[cpp] viewplaincopy
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (tableView == tableView_)
{
if (section == 0)
{
return @"title 1";
}
else if (section == 1)
{
return @"title 2";
}
else
{
return nil;
}
}
else
{
return nil;
}
}
(7) -(UIView *)tableView:(UITableView *)tableViewviewForHeaderInSection:(NSInteger)section 返回指定的
section header 的view,如果没有,这个函数可以不返回view
[cpp] viewplaincopy
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (section == 0)
{
UIView* header = [[[NSBundle mainBundle] loadNibNamed: @"SettingHeaderView"
owner: self
options: nil] lastObject];
else
{
return nil;
}
}
首先,Controller需要实现两个 delegate ,分别是 UITableViewDelegate 和 UITableViewDataSource
2.然后 UITableView对象的 delegate要设置为 self。
3. 然后就可以实现这些delegate的一些方法拉。
(1)- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
这个方法返回tableview 有多少个section
[cpp] viewplaincopy
//返回有多少个Sections
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
(2)- (NSInteger)tableView:(UITableView *)tablenumberOfRowsInSection:(NSInteger)section;这个方法返回
对应的section有多少个元素,也就是多少行
[cpp] viewplaincopy
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
(3)-(CGFloat)tableView:(UITableView *)tableViewheightForRowAtIndexPath:(NSIndexPath*)indexPath;
这个方法返回指定的 row 的高度。
- (CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section; 这个方法返回指定的
section的header view的高度。 - (CGFloat)tableView:(UITableView *)tableViewheightForFooterInSection:(NSInteger)section; 这个方法返回指定的section的footer
view 的高度。
(4)-(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath;返回指定的row的cell。这个地方是比较关键的地方,一般在这个地方来定制各种个性化的cell元素。这里只是使用最简单最基本 的cell
类型。其中有一个主标题cell.textLabel 还有一个副标题cell.detailTextLabel, 还有一个image在最前头叫 cell.imageView. 还可以设置右边的图标,通过cell.accessoryType 可以设置是饱满的向右的蓝色箭头,还是单薄的向右箭头, 还是勾勾标记。
[cpp] viewplaincopy
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * showUserInfoCellIdentifier = @"ShowUserInfoCell";
UITableViewCell * cell = [tableView_ dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier];
if (cell == nil)
{
// Create a cell to display an ingredient.
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:showUserInfoCellIdentifier]
autorelease];
}
// Configure the cell.
cell.textLabel.text=@"签名";
cell.detailTextLabel.text = [NSString stringWithCString:userInfo.user_signature.c_str() encoding:NSUTF8StringEncoding];
}
(5)-(CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section 返回指定的
section 的header的高度
[cpp] viewplaincopy
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
if (section ==0)
return 80.0f;
else
return 30.0f;
}
(6)- (NSString *)tableView:(UITableView *)tableViewtitleForHeaderInSection:(NSInteger)section
返回指定的section 的 header 的title,如果这个section header 有返回view,那么title就不起作用了
[cpp] viewplaincopy
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if (tableView == tableView_)
{
if (section == 0)
{
return @"title 1";
}
else if (section == 1)
{
return @"title 2";
}
else
{
return nil;
}
}
else
{
return nil;
}
}
(7) -(UIView *)tableView:(UITableView *)tableViewviewForHeaderInSection:(NSInteger)section 返回指定的
section header 的view,如果没有,这个函数可以不返回view
[cpp] viewplaincopy
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
if (section == 0)
{
UIView* header = [[[NSBundle mainBundle] loadNibNamed: @"SettingHeaderView"
owner: self
options: nil] lastObject];
else
{
return nil;
}
}
相关文章推荐
- [iphone开发]UIImageView常用操作
- UIVIEWCONTROLLER各个方法的加载顺…
- java09 队列Queue与Deque
- 利用sourceinsight宏(Quicker.em)提高编码效率和质量
- UIView动画animation
- 受用户喜爱的UI设计产品离不开数据分析
- UI设计中你的视觉感觉对你的设计的局限性
- UI设计师产品构思过程需要有一个系统思维方向
- 【UIKit-124-5】#import <UIKit/UIView.h>
- Building System之 get_abs_build_var() && get_build_var()
- APUE-第十四章 高级I/O
- 自定义TabBarController
- UITabBarController剖析
- UIKit入门
- 前端框架 EasyUI (1)熟悉一下EasyUI
- iOS:CoreData数据库的使用四(数据库和UITableViewController以及NSFetchedResultsController一起使用)
- 当UITextField被系统键盘挡住的时候如何解决
- 【UIKit-124-4】#import <UIKit/UIView.h>
- 优化UITableViewCell高度计算的那些事
- UIView动画