iOS UIKit:UITableView

来源:互联网 发布:mac versions 注册码 编辑:程序博客网 时间:2024/06/06 19:32

UITableView是UIScollView的子类,用于显示一列数据。

 

UITableView 的Style 预设有两种:Plain 及Grouped。

 

 

 

Plain:

Grouped:

(ps:多了个外边框)

 

装在UITableView 里面的元素是UITableViewCell。

 

而UITableViewCell 预设有4种样式Style:

UITableViewCellStyleDefault:预设使用这种,若左侧ImageView没图的话,只有一行字(textLable.text)。

UITableViewCellStyleValue1:左侧为textLable.text并且左对齐,右侧为detailTextLable.text并且右对齐。

UITableViewCellStyleValue2:左侧为detailTextLable.text,右侧为textLable.text并且左对齐。

UITableViewCellStyleSubtitle:跟UITableViewCellStyleDefault大致相同,detailTextLable.text出现在textLable.text下方。

 

 

UITableViewDataSource:

UITableViewDataSource 协议定了2个要求实现的方法(tableView:cellForRowAtIndexPath tableView:numberOfRowsInSection)。通过实现这些方法,你告诉表视图显示多少行数据和每一行中的数据。

numberOfSectionsInTableView:This method allows the data source to inform the table view of the number ofsections that must be loaded into the table.tableView:numberOfRowsInSection:This method tells the view controller how many cells or rows have to be loaded foreach section. The section number is passed to the data source in the numberOfRowsInSection parameter. The implementation of this method is mandatory in the datasource object.tableView:cellForRowAtIndexPath:This method is responsible for returning instances of the UITableViewCell class asrows that have to be populated into the table view. The implementation of thismethod is mandatory in the data source object

 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    return [model getRowCount];}- (UITableViewCell *)tableView:(UITableView *)tableView         cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *CellIdentifier = @"Cell";        UITableViewCell *cell = [tableView                             dequeueReusableCellWithIdentifier:CellIdentifier];    if (cell == nil) {        cell = [[UITableViewCell alloc]                initWithStyle:UITableViewCellStyleDefault                reuseIdentifier:CellIdentifier] ;    }        NSUInteger row = [indexPath row];    cell.textLabel.text = [model getNameAtIndex:row];    return cell;}

 

 

UITableViewDelegate:

负责处理UITableView的表现。

 协议中的可选方法让你管理表行的高度,配置节点头部和底部,对表单元重新排序等等。

- ( void ) tableView: ( UITableView * ) tableView didSelectRowAtIndexPath: ( NSIndexPath * ) indexPath {  if ([ tableView isEqual: self . myTableView ]){  NSLog ( @"%@" ,[ NSString stringWithFormat: @"Cell %ld in Section %ld is selected" ,( long ) indexPath . row , ( long ) indexPath . section ]);  }}

Constructing Headers and Footers in Table Views:

- ( UIView * ) tableView: ( UITableView * ) tableViewviewForHeaderInSection: ( NSInteger ) section {UILabel * result = nil ;if ([ tableView isEqual: self . myTableView ] &&section == 0 ){result = [[ UILabel alloc ] initWithFrame: CGRectZero ];result . text = @"Section 1 Header" ;result . backgroundColor = [ UIColor clearColor ];[ result sizeToFit ];}return result ;}- ( UIView * ) tableView: ( UITableView * ) tableViewviewForFooterInSection: ( NSInteger ) section {UILabel * result = nil ;if ([ tableView isEqual: self . myTableView ] &&section == 0 ){result = [[ UILabel alloc ] initWithFrame: CGRectZero ];result . text = @"Section 1 Footer" ;result . backgroundColor = [ UIColor clearColor ];[ result sizeToFit ];}return result ;

源码下载


<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>