IOS类似iphone通讯录TableView的完整demo【附源码】

来源:未知 责任编辑:智问网络 发表时间:2013-09-22 21:36 点击:
初学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://www.2cto.com/uploadfile/2012/0525/20120525022534806.zip
 

 

 

摘自 dongge_111的专栏

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • Lexical or Preprocessor Issue 'xxx.h
    • ios学习笔记(二)xcode 4.3.2下实现基本交互
    • ios版本的helloworld
    • iphone(object-c) 内存管理(3) 有效的内存管理 前半部分
    • ios学习笔记(一)xcode 4.3.2下创建第一个ios项目
    • IOS类似iphone通讯录TableView的完整demo【附源码】
    • UITableView一些方法
    • [iPhone中级]iPhone团购信息客户端的开发 (二)
    • 如何为Iphone应用创建启动界面
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1