初學objective-c,自己寫的一個demo,有不足之處請指正:
效果圖:
核心代碼:
[plain]
– (void)viewDidLoad
{
[super viewDidLoad];
NSArray *array = [[NSArray alloc] initWithObjects:@"你好", @"BFlower",
@"CGrass", @"DFence", @"EHouse", @"FTable", @"GChair",
@"HBook", @"ISwing" ,@"JWang" ,@"KDong" ,@"LNi" ,@"MHao" ,@"Na" ,@"Oa" ,@"Pa" ,@"Qa" ,@"Ra" ,@"Sa" ,@"Ta" ,@"Ua" ,@"Va" ,@"Wa" ,@"Xa" ,@"Ya" ,@"Za", nil];
self.listarray = array;
NSLog(@"listarryCount:%d",[listarray count]);
secLabelArray = [[NSArray alloc] initWithObjects:@"A", @"B", @"C",@"D", @"E", @"F",@"G", @"H", @"I",@"J", @"K", @"L",@"M", @"N", @"O",@"P", @"Q", @"R",@"S", @"T", @"U",@"V", @"W", @"X",@"Y", @"Z", nil];
NSArray *arrayA = [[NSArray alloc] initWithObjects:@"測試A1",@"測試A2", nil];
NSArray *arrayB = [[NSArray alloc] initWithObjects:@"測試B1",@"測試B2",@"測試B3", nil];
NSArray *arrayC = [[NSArray alloc] initWithObjects:@"測試C1",@"測試C2",@"測試C3",@"測試C4", nil];
NSArray *arrayD = [[NSArray alloc] initWithObjects:@"測試D1",@"測試D2",@"測試D3",@"測試D4",@"測試D5", nil];
NSArray *arrayE = [[NSArray alloc] initWithObjects:@"測試E1",@"測試E2",@"測試E3",@"測試E4",@"測試E5",@"測試E6", nil];
NSArray *arrayF = [[NSArray alloc] initWithObjects:@"測試F1",@"測試F2",@"測試F3",@"測試F4",@"測試F5",@"測試F6",@"測試F7", nil];
NSArray *arrayG = [[NSArray alloc] initWithObjects:@"測試G1",@"測試G2",@"測試G3",@"測試G4",@"測試G5",@"測試G6", nil];
arrayDictKey = [[NSArray alloc] initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G", nil];
arrayDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:arrayA,[arrayDictKey objectAtIndex:0],
arrayB,[arrayDictKey objectAtIndex:1],
arrayC,[arrayDictKey objectAtIndex:2],
arrayD,[arrayDictKey objectAtIndex:3],
arrayE,[arrayDictKey objectAtIndex:4],
arrayF,[arrayDictKey objectAtIndex:5],
arrayG,[arrayDictKey objectAtIndex:6],
nil];
NSLog(@"arrayrow:%d",[[arrayDict objectForKey:[arrayDictKey objectAtIndex:1]] count]);
tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
[tableView setDelegate:self];
[tableView setDataSource:self];
[self.view addSubview:tableView];
[tableView release];
// Do any additional setup after loading the view.
}
– (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView
{
//* 出現幾組
//if(aTableView == self.tableView) return 27;
return [arrayDict count];
}
//*字母排序搜索
– (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
//* 字母索引列表
/*NSMutableArray *toBeReturned = [[NSMutableArray alloc]init];
for(char c= 'A';c<='Z';c++)
[toBeReturned addObject:[NSString stringWithFormat:@"%c",c]];*/
return arrayDictKey;
/*NSMutableArray *newarr=[[NSMutableArray alloc]initWithArray:listarray];
[newarr addObject:@"{search}"]; //等價於[arr addObject:UITableViewIndexSearch];
return newarr;*/
}
– (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
//搜索時顯示按索引第幾組
NSInteger count = 0;
NSLog(@"%@",title);
for(NSString *character in arrayDictKey)
{
if([character isEqualToString:title])
{
return count;
}
count ++;
}
return count;
}
– (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
/*if([listarray count]==0)
{
return @"";
}*/
//return [listarray objectAtIndex:section]; //*分組標簽
return [arrayDictKey objectAtIndex:section];
}
– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//return [self.listarray count]; //*每組要顯示的行數
//NSInteger i = [[listarray objectAtIndex:section] ]
NSInteger i = [[arrayDict objectForKey:[arrayDictKey objectAtIndex:section]] count];
return i;
}
-(UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath{
//返回類型選擇按鈕
return UITableViewCellAccessoryDisclosureIndicator; //每行右邊的圖標
}
– (UITableViewCell *)tableView:(UITableView *)tableview
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *TableSampleIdentifier = @"TableSampleIdentifier";
UITableViewCell *cell = [tableview dequeueReusableCellWithIdentifier:
TableSampleIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:TableSampleIdentifier];
}
NSUInteger row = [indexPath row];
NSUInteger sectionMy = [indexPath section];
NSLog(@"sectionMy:%d",sectionMy);
cell.textLabel.text = [[arrayDict objectForKey:[arrayDictKey objectAtIndex:sectionMy]] objectAtIndex:row]; //每一行顯示的文字
NSString *str= [NSString stringWithFormat: @"%d", row];
UIImage *image = [UIImage imageNamed:str];
cell.imageView.image = image;
UIImage *highLighedImage = [UIImage imageNamed:@"1.png"];
cell.imageView.highlightedImage = highLighedImage; //選中一行時頭部圖片的改變
return cell;
}
//劃動cell是否出現del按鈕
– (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
return YES; //是否需要刪除圖標
}
//編輯狀態(不知道幹什麼用)
– (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
[self viewDidLoad];
}
//選中時執行的操作
– (NSIndexPath *)tableView:(UITableView *)tableView
willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger row = [indexPath row];
if (row%2 == 0) {
NSUInteger row = [indexPath row];
NSString *rowValue = [listarray objectAtIndex:row];
NSString *message = [[NSString alloc] initWithFormat:
@"You selected %@", rowValue];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Row Selected!"
message:message
delegate:nil
cancelButtonTitle:@"Yes I Did"
otherButtonTitles:nil];
[alert show];
}
return indexPath;
}
– (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//這裡控制值的大小
return 50.0; //控制行高
}
總結:有些東西沒用之前覺得這個會很難,很復雜,做過後才發現,其實關鍵在於理解,多看多實踐這樣才能提高…多看看我的demo,研究一下哈…
demo下載地址:http://up.aiwalls.com/2012/0525/20120525022534806.zip
摘自 dongge_111的專欄