原文地址: http://blog.sina.com.cn/s/blog_7e3132ca0100wyls.html
在XCode對(duì)應(yīng)頭文件中修改該類所繼承的父類:
@interface TableViewController:UIViewController <UITableViewDataSource, UITableViewDelegate>
{
}
在對(duì)應(yīng)的.m文件中添加如下代碼:
@implementation TableViewController
{
UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0,320,460) style:UITableViewStylePlain];
tableView.dataSource = self;
tableView.delegate = self;
[self.view addSubview:tableView];
}
這樣就在view上添加了一個(gè)tableView,但其樣式是默認(rèn)的,其中的內(nèi)容也是空白的,而且此時(shí)是無(wú)法運(yùn)行的,因?yàn)樵陬^文件中添加了UITableViewDataSource和UITableViewDelegate兩個(gè)類,所以必須設(shè)置一些自定義tableView樣式的方法,下面列舉了一些相關(guān)的方法:
設(shè)置Cell高度:
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
設(shè)置SectionHeader高度:
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
設(shè)置SectionFooter高度:
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
設(shè)置Section數(shù)目:
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
設(shè)置SectionHeader內(nèi)容:
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
設(shè)置各個(gè)Section中的Cell個(gè)數(shù):
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
設(shè)置Cell內(nèi)容:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
設(shè)置Cell行縮進(jìn)量:
-(NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath
設(shè)置Cell被選中響應(yīng)前動(dòng)作(例如:可用以判斷選中的Cell,來(lái)阻止其響應(yīng))
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
設(shè)置Cell選中觸發(fā)響應(yīng):
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
posted on 2011-12-29 09:12
Robin's Programming World 閱讀(2333)
評(píng)論(1) 編輯 收藏 所屬分類:
其它