??UITableView?*DataTable;
? ??NSMutableArray?*dataArray1;
? ??NSMutableArray?*dataArray2;
? ??NSMutableArray?*titleArray;
? ??NSMutableArray?*dataArray;?//加入了用于保存數組的數組 dataArray
}
- (void)viewDidLoad
{
? ? [superviewDidLoad];
? ??DataTable?= [[UITableViewalloc]?initWithFrame:CGRectMake(0,?0,?320,?420)];
? ? [DataTablesetDelegate:self];
? ? [DataTablesetDataSource:self];
? ? [self.viewaddSubview:DataTable];
? ??dataArray1?= [[NSMutableArrayalloc]?initWithObjects:@"中國",?@"美國",?@"英國",?nil];
? ??dataArray2?= [[NSMutableArrayalloc]?initWithObjects:@"黃種人",?@"黑種人",?@"白種人",?nil];
? ??titleArray?= [[NSMutableArrayalloc]?initWithObjects:@"國家",?@"種族",?nil];
? ??dataArray?= [[NSMutableArrayalloc]?initWithObjects:dataArray1,?dataArray2,?nil];?//初始化dataArray,元素為數組
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
? ??// Return YES for supported orientations
? ??return?(interfaceOrientation ==?UIInterfaceOrientationPortrait);
}
?//制定個性標題,這里通過UIview來設計標題,功能上豐富,變化多。
- (UIView?*)tableView:(UITableView?*)tableView viewForHeaderInSection:(NSInteger)section {
? ??UIView?*view = [[[UIViewalloc]?initWithFrame:CGRectMake(0,?0,?320,?40)]?autorelease];
? ? [view?setBackgroundColor:[UIColorbrownColor]];//改變標題的顏色,也可用圖片
? ??UILabel?*label = [[UILabelalloc]?initWithFrame:CGRectMake(5,?5,?100,?30)];
? ? label.textColor?= [UIColorredColor];
? ? label.backgroundColor?= [UIColorclearColor];
? ? label.text?= [titleArrayobjectAtIndex:section];
? ? [view?addSubview:label];
? ???return?view;
}
?//指定標題的高度
- (CGFloat)tableView:(UITableView?*)tableView heightForHeaderInSection:(NSInteger)section{
? ??return 40;
}
//每個section顯示的標題,有了上面的這個就不要了
- (NSString?*)tableView:(UITableView?*)tableView titleForHeaderInSection:(NSInteger)section{
}
//指定有多少個分區(Section),默認為1
- (NSInteger)numberOfSectionsInTableView:(UITableView?*)tableView {
? ??return?[titleArraycount];
}
//指定每個分區中有多少行,默認為1
- (NSInteger)tableView:(UITableView?*)tableView numberOfRowsInSection:(NSInteger)section{
???/* switch (section) {
? ? ? ? case 0:
?? ? ? ? ? return? [dataArray1 count];
? ? ? ? ? ? break;
? ? ? ? case 1:
? ? ? ? ? ? return? [dataArray2 count];
? ? ? ? ? ? break;
? ? ? ? default:
? ? ? ? ? ? return 0;
? ? ? ? ? ? break;
? ? }*/
??/*? for(int i = 0; i < [titleArray count]; i++){
? ? ? ? if(section == i){
? ? ? ? ? ? return [[dataArray objectAtIndex:section] count];
? ? ? ? }
? ? }*/
//上面的方法也是可行的,大家參考比較下
? ??return?[[dataArray objectAtIndex:section]?count]; ?//取dataArray中的元素,并根據每個元素(數組)來判斷分區中的行數。
}
//繪制Cell
-(UITableViewCell?*)tableView:(UITableView?*)tableView cellForRowAtIndexPath:(NSIndexPath?*)indexPath {
? ??static?NSString?*CellIdentifier =?@"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]];
break;
case 1:
[[cell textLabel]?
setText:[dataArray2 objectAtIndex:indexPath.row]];
break;
default:
[[cell textLabel]?
setText:@"Unknown"];
}*/
? ??//上面的方法也可行,大家比較下。
? ? [[cell?textLabel]?setText:[[dataArrayobjectAtIndex:indexPath.section]objectAtIndex:indexPath.row]];
?//同上,取出dataArray中每個分區所對應的元素(數組),并通過其來取值。 (大家要有想像力, 復制代碼試試就明白了)
return?cell;
}
?//改變行的高度
- (CGFloat)tableView:(UITableView?*)tableView heightForRowAtIndexPath:(NSIndexPath?*)indexPath{
? ??return40;
}