该主题主要讲述uitableview的基本属性的设置:
//创建UITableView
//设置table的类型:UITableViewStylePlain为普通列表模式,UITableViewStyleGrouped为分组模式/*
typedef NS_ENUM(NSInteger, UITableViewStyle) {
UITableViewStylePlain, // regular table view
UITableViewStyleGrouped // preferences style table view
};
*/
_table = [[[UITableView alloc]initWithFrame:CGRectMake(100, 100, 200, 200) style:UITableViewStylePlain]autorelease];
//设置table的水平指示,该属性继承自UIScrollView
_table.showsHorizontalScrollIndicator = YES;//设置table的垂直指示,该属性继承自UIScrollView
_table.showsVerticalScrollIndicator = YES;
_table.allowsSelection = YES;
UIImageView* bg = [[UIImageView alloc]initWithFrame:_table.frame];
bg.image = [UIImage imageNamed:@"bg@2x.png"];
_table.backgroundView = bg;
[bg release];
_table.tableHeaderView = nil;
_table.tableFooterView = nil;
_table.rowHeight = 50.f;
//设置table的section的header和footer的高度,也可以通过delegate的回调设置
_table.sectionHeaderHeight = 20.f;
_table.sectionFooterHeight = 20.f;
/*默认是UITableViewCellSeparatorStyleSingleLine,UITableViewCellSeparatorStyleNone是不显示分割线,UITableViewCellSeparatorStyleSingleLineEtched只能用于table的style是group分组模式
typedef NS_ENUM(NSInteger, UITableViewCellSeparatorStyle) {
UITableViewCellSeparatorStyleNone,
UITableViewCellSeparatorStyleSingleLine,
UITableViewCellSeparatorStyleSingleLineEtched // This separator style is only supported for grouped style table views currently
};
*/
_table.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
_table.separatorColor = [UIColor blueColor];
_table.sectionIndexColor = [UIColor yellowColor];
//设置table的右边索引兰的背景颜色
_table.sectionIndexTrackingBackgroundColor = [UIColor grayColor];
_table.delegate = self;
_table.dataSource = self;
[self.view addSubview:_table];
设置tableview常用回调和datasource
//datasource
//设置每个section的cell行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)
//返回table的cell对象,只有当cell被滑动到table的可视范围内才被回调
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString* indentifier = @"cell";
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
if (!cell) {
/*默认cell的类型
typedef NS_ENUM(NSInteger, UITableViewCellStyle) {
UITableViewCellStyleDefault, // Simple cell with text label and optional image view (behavior of UITableViewCell in iPhoneOS 2.x)
UITableViewCellStyleValue1, // Left aligned label on left and right aligned label on right with blue text (Used in Settings)
UITableViewCellStyleValue2, // Right aligned label on left with blue text and left aligned label on right (Used in Phone/Contacts)
UITableViewCellStyleSubtitle // Left aligned label on top and left aligned label on bottom with gray text (Used in iPod).
};
*/
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier]autorelease];
//设置自定义的cell被选中后的view
UIView* selected = [[UIView alloc]initWithFrame:cell.frame];
selected.backgroundColor = [UIColor redColor];
cell.selectedBackgroundView =selected;
}
cell.textLabel.text = @"text";
//设置cell的被选中后style
/*
typedef NS_ENUM(NSInteger, UITableViewCellSelectionStyle) {
UITableViewCellSelectionStyleNone,
UITableViewCellSelectionStyleBlue,
UITableViewCellSelectionStyleGray
};
*/
//cell.selectionStyle = UITableViewCellSelectionStyleBlue;
return cell;
}
//设置table的section个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
} // Default is 1 if not implemented
//设置设置每个section的header头部显示的文字内容,也可以使用自定义view的回调
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return @"textH";
} // fixed font style. use custom view (UILabel) if you want something different
//设置设置每个section的footer尾部显示的文字内容,也可以使用自定义view的回调
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
return @"textF"
}
//返回由section的所有title组成的index索引,在table右边的提示索引
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return [NSArray arrayWithObjects:@"1",@"2",@"3", nil];
}
//delegate
//回调设置table每行cell的高度,会覆盖table.rowHeight的设置
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}
//回调设置table每个section的header的高度,会覆盖table.sectionHeaderHeight的设置
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 20;
}
//回调设置table每个section的footer的高度,会覆盖table.sectionFooterHeight的设置
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return 20;
}
//
Section header & footer information. Views are preferred over title should you decide to provide both
//
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
return nil;
} // custom view for header. will be adjusted to default or specified header height
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
return nil;
} // custom view for footer. will be adjusted to default or specified footer height