现在的位置: 首页 > 综合 > 正文

UITableView Cell Index和数据库的匹配套用

2013年03月18日 ⁄ 综合 ⁄ 共 3148字 ⁄ 字号 评论关闭

前几天纠结自己不怎么会使用UItableview 现在整理如下

UItableview 要想实现完整的内容,要遵循两个协议,除此之外,设置Uitableview 在这个之前,已经自定义一个类uitableview的设置属性,所以,直接展示几个关键的方法。

[self.tableView setSectionHeaderHeight:40.0];
[self.tableView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"beijing.png"]]];
[self.tableView setSeparatorColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"dividing-line@2x.png"]]];
[self.tableView setSectionIndexColor:[UIColor brownColor]];

以上是所有相关的tableview的设置,在这里,在H里面将tableview设置为了属性,好处,方便使用调用(在多个类中),第二个:减少了relese 的麻烦

_buShouArray=[[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17", nil];
_bsMinCh=[[NSArray alloc]initWithObjects:@"笔画一",@"笔画二",@"笔画三",@"笔画四",@"笔画五",@"笔画六",@"笔画七",@"笔画八",@"笔画九",@"笔画十",@"笔画十一"@"笔画十二",@"笔画十三",@"笔画十四",@"笔画十五",@"笔画十六",@"笔画十七", nil];

以下是必须遵守的两个协议:

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [_headerArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * inidentifier=@"cell";
UITableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:inidentifier];
if (cell==nil) {
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:inidentifier]autorelease];
}
strArr=[_headerArray objectAtIndex:indexPath.section];
pysStr=[[Pinyin selectPinyinByType:strArr]objectAtIndex:indexPath.row];
cell.textLabel.text=pysStr.pinyin;

return cell;

}

自定义UItableview的每个分区的header

//每个分区的头部
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [_headerArray objectAtIndex:section];
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIButton * headButton=[Utility addButtonWithType:UIButtonTypeCustom andTag:9 andFrame:CGRectMake(0, 0, 320, 40) andTitle:nil andBackgroundImage:[UIImage imageNamed:@"pinyin@2x.png"] andFuction:nil andDelegate:self andView:self.tableView];
[headButton.titleLabel setTextAlignment:NSTextAlignmentLeft];
UILabel * label =[Utility addLabelWithTitle:[_headerArray objectAtIndex:section] andFrame:CGRectMake( 13,-15, 100, 70) andView:headButton];
[label setTextAlignment:NSTextAlignmentLeft];
label.textColor=[UIColor brownColor];
label.font=[UIFont fontWithName:@"Arial" size:30.0];
label.backgroundColor =[UIColor clearColor];
return headButton;
}

设置每个分区的内容

//设定每个分区的行数以及行数的显示内容
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

NSString * pyStr =[_headerArray objectAtIndex:section];
return [[Pinyin selectPinyinByType:pyStr]count];
}

在这里面,实质上展示出的是部分的内容因为要和data相关Pinyin就是导出database的类的名称,从database中根据type属性选择相关的字节,展示在每个section里面。

设置在右部展示的index

-(NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return _headerArray;
}

相关联接下来的另外一个tableview,用这个方法关联

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
RootDetailViewController * chaZi=[[RootDetailViewController alloc]init];
NSString * stAr=[_headerArray objectAtIndex:indexPath.section];
Pinyin * bss=[[Pinyin selectPinyinByType:stAr]objectAtIndex:indexPath.row];
chaZi.strText=bss.pinyin;
//NSLog(@"bss=%@",bss.title);
[self.navigationController pushViewController:chaZi animated:YES];
[chaZi release];
}

抱歉!评论已关闭.