iOS UIKit:UITableView

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 of
sections that must be loaded into the table.

tableView:numberOfRowsInSection:
This method tells the view controller how many cells or rows have to be loaded for
each section. The section number is passed to the data source in the numberOfRows
InSection parameter. The implementation of this method is mandatory in the data
source object.

tableView:cellForRowAtIndexPath:
This method is responsible for returning instances of the UITableViewCell class as
rows that have to be populated into the table view. The implementation of this
method 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 * ) tableView
viewForHeaderInSection: ( 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 * ) tableView
viewForFooterInSection: ( 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"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值