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>