UITableViewDataSource
#pragma mark - UITableViewDataSource //一共有多少组(可以不写,默认为1组) - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } //每个组有多少数据 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 10; } //每组数据如何显示 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell_ABC"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier] ; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; //使用xib自定义时使用 //cell = [[[NSBundle mainBundle] loadNibNamed:@"XYContentCell" owner:self options:nil] lastObject]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } cell.textLabel.text = @"123"; return cell ; }
获取UITableView 内部高度(Content Height)
- (CGFloat)tableViewHeight { [tableView layoutIfNeeded]; return [tableView contentSize].height; }
UITableView 某一行 默认是 选中 状态
NSIndexPath *index0 = [NSIndexPath indexPathForRow:0 inSection:0]; [self.tableView selectRowAtIndexPath:index0 animated:YES scrollPosition:UITableViewScrollPositionBottom];
UITableView 滑动到指定位置
NSArray *visibleRows = [self.mTableView indexPathsForVisibleRows]; if (visibleRows.count > 0) { NSIndexPath *visibleIndexPath = visibleRows[0]; if (visibleIndexPath.row == indexPath.row) { //滑动到 指定位置 [self.mTableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
UISwitch in a UITableView cell (UITableViewCell里镶嵌UISwitch)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { switch( [indexPath row] ) { case MY_SWITCH_CELL: { UITableViewCell* aCell = [tableView dequeueReusableCellWithIdentifier:@"SwitchCell"]; if( aCell == nil ) { aCell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"SwitchCell"] autorelease]; aCell.textLabel.text = @"I Have A Switch"; aCell.selectionStyle = UITableViewCellSelectionStyleNone; UISwitch *switchView = [[UISwitch alloc] initWithFrame:CGRectZero]; aCell.accessoryView = switchView; [switchView setOn:NO animated:NO]; [switchView addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged]; [switchView release]; } return aCell; } break; } return nil; } - (void) switchChanged:(id)sender { UISwitch* switchControl = sender; NSLog( @"The switch is %@", switchControl.on ? @"ON" : @"OFF" ); }
自定义UITableView的Header
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { // create the parent view that will hold header Label UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320, 20.0)]; UIImageView *bg = [[UIImageView alloc]initWithFrame:customView.frame]; bg.image = [UIImage imageNamed:@"carTypeCellTitleBg1.png"]; [customView addSubview:bg]; // create the button object UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero]; headerLabel.backgroundColor = [UIColor clearColor]; headerLabel.opaque = NO; headerLabel.textColor = [UIColor colorWithRed:242.0/255.0f green:161.0/255.0f blue:4.0/255.0 alpha:1.0]; // headerLabel.highlightedTextColor = [UIColor whiteColor]; headerLabel.font = [UIFont italicSystemFontOfSize:15]; headerLabel.frame = customView.frame; // If you want to align the header text as centered // headerLabel.frame = CGRectMake(150.0, 0.0, 300.0, 44.0); // headerLabel.text = <Put here whatever you want to display> // i.e. array element headerLabel.text = @"title"; [customView addSubview:headerLabel]; return customView; } - (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 21.0; }
1.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
方法要比
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 方法
先返回
即,高度比cell填充先返回
2.
xib自定义Cell,复用无效;需要用代码重写
再一次验证,xib自定义的Cell可以复用
有两种方式,
第一种:
在自定义的Cell里面重写reuseIdentifier方法
- (NSString*)reuseIdentifier { //返回的是在- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;方法里定义的reuseIdentifier return @"ContentCELL"; }
第二种:
在cell的xib文件里定义
相关推荐
UITableView、UITableView基本用法、UITableView详解,UITabelView常用方法
UITableView相关操作
UITableView Cache - UITableView 缓存
UITableView教材 IPHONE IPAD
IOS UITableView UIcollectionView 结合
UITableView 详细讲解
UITableView用来以表格的形式显示数据。关于UITableView,我们应该注意: (1)UITableView用来显示表格的可见部分,UITableViewCell用来显示表格的一行。 (2)UITableView并不负责存储表格中的数据,而是仅仅存储...
UITableView通过重用单元格来达到节省内存的目的:通过为每个单元格指定一个重用标识符 reuseIdentifier 即指定了单元格的种类 以及当单元格滚出屏幕时 允许恢复单元格以便重用 对于不同种类的单元格使用不同的ID ...
UITableView 缩进 附属视图 Cell Demo
iOS UITableView查询及索引UISEARCHBAR
UITableView 三级菜单
这是iPhone之UITableView入门的示例程序,具体参考: http://blog.csdn.net/htttw/article/details/7875462
UITableView的简单使用
UITableview处理键盘遮挡 没有实现控件复用导致的出现重复文本,以及部分bug。
UITableView的SectionHeader的复用,UITableView的SectionHeader的复用
UICollectionView,UITableView用法,UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
UITableView 删除 Cell Demo Delete
UITableView Demo UITableCell 代码 表格
UITableView 简单例子,显示两个表区域,然后点击每行弹出一个提示框,有代码注释