1. TableView 样式
TableView有两种样式,可以在初始化的时候选择Plain或者Grouped
UITableView *tview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, self.view.frame.size.height) style:UITableViewStyleGrouped];
2. 委托方法(tableview delegate)
sectionArrays是所有数据的集合 NSMutableArray
2.1 分组(必须实现的方法)
2.1.1. TableView分为多少个section(默认返回1)
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return [titleArray count];//返回标题数组中元素的个数来确定分区的个数
- }
2.1.2 每个section有多少个cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
2.1.3 每个Cell的样式
- -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- static NSString *CellIdentifier = @"Cell";
- //初始化cell并指定其类型,也可自定义cell
- UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if(cell == nil)
- {
- cell = [[[UITableViewCellalloc]
- initWithFrame:CGRectZero
- reuseIdentifier:CellIdentifier] autorelease];
- }
- switch (indexPath.section) {
- case 0://对应各自的分区
- [[cell textLabel] setText:[dataArray1 objectAtIndex:indexPath.row]];//给cell添加数据
- break;
- case 1:
- [[cell textLabel] setText:[dataArray2 objectAtIndex:indexPath.row]];
- break;
- default:
- [[cell textLabel] setText:@"Unknown"];
- }
- return cell;//返回cell
- }
2.2 Section Header
2.2.1 单独设置某个Section Header内容
- - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
- NSString *provincName = [[provinces objectAtIndex:section] objectForKey:@"p_Name"];
- return provincName;
- }
- - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
- //返回省份的数组
- NSMutableArray *array = [NSMutableArray arrayWithCapacity:35];
- for (NSDictionary *dict in provinces) {
- [array addObject:[dict objectForKey:@"p_Name"]];
- }
- return array;
- }
- - (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 display to want you whatever here>// i.e. array element
- headerLabel.text = @"title";
- [customView addSubview:headerLabel];
- return customView;
- }
2.2.4 设置Section Header高度
- - (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- {
- return 21.0;
- }
2.3 Section Footer
2.3.1 设置Footer内容
-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
NSString *result = nil;
if ([tableView isEqual:myTableView]&§ion==0) {
result = @"Section 0 Header";
}
return result;
}
2.3.2 自定义Footer样式
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if (section == 0)
{
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"我是button 1"forState:UIControlStateNormal ];
return button;
}
else
{
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"我是button 2"forState:UIControlStateNormal ];
return button;
}
}
2.3.3 自定义Footer高度
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 20.0;
}
2.4 Cell
这篇文章中的例子没有尝试过,但是很有用,可以看看 http://blog.csdn.net/totogo2010/article/details/7698767
2.4.1 Cell点击事件的监听
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// 取消cell被选中效果
[tableView deselectRowAtIndexPath:indexPath animated:YES];
NSString *titileString = [arrayobjectAtIndex:[indexPathrow]]; //这个表示选中的那个cell上的数据
UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:titileStringdelegate:selfcancelButtonTitle:@"OK"otherButtonTitles:nil];
[alert show];
}
2.4.2 改变Cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 40;
}
2.4.3 点击某个Cell无效
-(NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//第一个cell 点击的时候没有反应
if (indexPath.row ==0) {
returnnil;
}
return indexPath;
}
给 TableView 设置背景
1. 定义UIImageView
2. tableView.setBackgroundView即可
要记住,在初始化Cell时,要设置[cell setbeckgroundColor:[UIColor clearColor]],否则会默认Cell背景为白色
取消 TableView Cell的分隔线
[self.myTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
解决滑动时Cell内容重叠问题
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil]; }
reuseIdentifier千万不能复用原来的identifier,一定设为nil
取消 TableView Cell的选中高亮效果
cell.selectedBackgroundView=[[UIView alloc] init];
cell.selectedBackgroundView.backgroundColor=[UIColor clearColor];
选中 TableView Cell时改变字体颜色
直接在label中设置:label.highlightedTextColor=[xxxxxx];
但是要注意,撤销[tableView deselectRowAtIndexPath:indexPath animated:YES];这句代码
默认选中 TableView Cell的第一行
NSIndexPath *path=[NSIndexPath indexPathForItem:0 inSection:0];
[tableview selecRowAtIndexPath:path animated:YES scrollPosition:UITableViewScrollPositionButtom];
TableViewCell 样式
UITableViewCellAccessoryNone, 空
UITableViewCellAccessoryDisclosureIndicator, >
UITableViewCellAccessoryDetailDisclosureButton, ">"按钮
UITableViewCellAccessoryCheckmark √
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;