ios-利用UITableView实现简单的通讯录(纯代码实现)

利用UITableView实现简单的通讯录

在实现此功能之前,先简单的了解UITableView的方法使用:

发现有几个博客讲的很不错,贴出来推荐:https://blog.csdn.net/mhw19901119/article/details/8755029

一、UITableView的基本使用方法:

主要的核心实现由三个部分组成:

(1)设置并返回此TableView是由多少个section组成的。(即分段显示还是单个列表显示)两者的区别就是普通通讯录和微信列表的通讯录

-(NSInteger)numberOfSectionsInTableView:(UITableView *) tableview  
{  
    return 1;//返回有多少个section  ,这里可以是数组,也可以是数据源的多少,例如NSArray中数据的个数。
} 

(2)设置每个分区的行数,可以直接返回数字也可以返回数据源的多少。对于不同分区想要展示的行数不同时,可以使用switch.

-(NSInteger)tableView:(UITableView *)tableView  numberOfRowInSection:
(NSInteger *)indexPath
{
   return 10;//返回对应section(indexPath)有多少元素(行数)
}

(3)设置并返回每一个cell,可以在这里对cell进行个性化的设置,其中设置主标题cell.textLable,一个副标题cell.detailTextLable,还有一个 image在最前头cell.imageView,还可以通过cell.accessoryType设置右边的图标(饱满的蓝色箭头,还是勾勾标记)。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *cellIdentifier = @"showUserInfoCell";
   UITableViewCell *cell = [tableView  dequeueReusableCellWithIdentifier:celldentifier] autorelease];
//判断cell是否存在,如果不存在就创建一个新的cell给它用。
   if(cell == nil){
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:
             cellIdentifier] autorelease];
   }
   //configure the cell
   cell.textLable.text = @"";//设置主标题
   cell.detailTextLable.text = @"";//设置副标题
}

二、UITableView的委托方法:

1、设置每个cell的高度(通用的方法):

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:
(NSIndexPath *)indexPath
{
  return 70.0f;
}

2、设置header seaction的高度:

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
  if(section == 0)
     return 50.0f;
  else
     return 30.0f;
}

3、设置footer seaction的高度:

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
  return 20.0f;
}

4、设置header title。如果这个section header有返回view,那么title设置无效

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:section
{
    if(tableView == _tableView){
         if(section == 0)
            return @"";//添加title的内容
          else if
             return @"";
          else
             return nil;
    }else{
        return nil;
    }
}

三、通讯录的实现:(通过上面函数实现,没有自己实现frame)下面屏蔽掉的是相关的一些函数学习。

viewController.m文件:

//
//  mainViewController.m
//  pnmlist
//
//  Created by niemin on 2018/3/26.
//  Copyright © 2018年 niemin. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "ViewController.h"

@interface ViewController ()

@end

NSArray* FirstList;
NSArray* LetterList;
NSArray* ANameList;
NSArray* BNameList;
NSArray* CNameList;
NSArray* DNameList;

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    // 分区数据
    FirstList = @[@"新的朋友",@"群聊",@"标签",@"联系人"];
    
    LetterList = @[@"",@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H",@"I"];
    
    ANameList = @[@"安斌",@"安茜",@"昂超",@"a~遇见"];
    
    BNameList = @[@"白丹",@"边柯",@"边晨"];
    
    CNameList = @[@"蔡鹏",@"曹温馨",@"程星河",@"寸子明"];
    
    DNameList = @[@""];
    
    // 创建UITableView
    UITableView* tableView =  [[UITableView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame style:UITableViewStyleGrouped];
    
    UILabel* headerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
    [headerLabel setText:@"通讯录"];
    //UIFont *font = [UIFont systemFontOfSize:24];//设置字体的大小
    headerLabel.textAlignment = NSTextAlignmentCenter;
    //设置UITable头信息
    [tableView setTableHeaderView:headerLabel];
    
    UILabel* footerLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 30, 320, 30)];
    [footerLabel setText:@"40位联系人"];
    footerLabel.textAlignment = NSTextAlignmentCenter;
    //设置UITable尾部信息
    [tableView setTableFooterView:footerLabel];
    
    [tableView setBackgroundColor:[UIColor whiteColor]];
    
    // 添加UITableView
    [self.view addSubview:tableView];
    
    // 设置数据源代理,必须实现协议UITableViewDataSource中的相关方法
    tableView.dataSource = self;//设置数据源
    tableView.delegate;//设置代理
    
}

//返回 tableView上的分区数量
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [LetterList count];
}

// 返回分区的title(分区是从 0 开始的)
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    // 根据分区的获取对应的名称
    return LetterList[section];//返回分区中的内容
}

// 返回tableView中的分区中的数据的数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    // 根据分区,获取分区中对应要显示的数据长度
    if(section == 0){
        return [FirstList count];//返回数组对象中的数量
    }else if(section == 1){
        return [ANameList count];
    }else if(section == 2){
        return [BNameList count];
    }else if(section == 3){
        return [CNameList count];
    }else{
        return 0;
    }
}

// 可重用标识符
static NSString* cellID = @"cellID";

// 将提供 tableView 中显示的数据
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    // 根据cellID获取可重用的UITableViewCell对象
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if(!cell){
        //创建一个UITableViewCell对象,并绑定到cellID
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    // 根据分区设置UITableViewCell显示的数据
    if(indexPath.section == 0){
        cell.textLabel.text = FirstList[indexPath.row];
        // 设置UITableViewCell的左边的图标
        //cell.imageView.image = [UIImage imageNamed:@"1.jpg"];//会将图片缓存在系统中,不适合占据内存较大的图片
        //cell.imageView.
        //cell.imageView.frame = CGRectMake(320,30,10,10);
        /*
        UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"1.jpg"]];
        imageView.frame = CGRectMake(60,60,100,100);
        imageView.layer.masksToBounds = YES;
        imageView.layer.cornerRadius = 50;
        [self.view addSubview:imageView];//可以将图片设置成圆形
         */
        /*
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(50,50,150,100);
        button.backgroundColor = [UIColor clearColor];
        [self.view addSubview:button];
         */
        
    }else if(indexPath.section == 1){
        cell.textLabel.text = ANameList[indexPath.row];
       // cell.imageView.image = [UIImage imageNamed:@"1.jpg"];
        NSString *filepath = [[ NSBundle mainBundle] pathForResource:@"1"ofType:@"jpg"];
        cell.imageView.image = [ UIImage imageWithContentsOfFile:filepath];//不会将图片缓存在系统中,不适合重复使用的图片。(一般情况下使用)
        
    }else if( indexPath.section == 2){
        cell.textLabel.text = BNameList[indexPath.row];
        NSString *filepath = [[ NSBundle mainBundle] pathForResource:@"1"ofType:@"jpg"];
        cell.imageView.image = [ UIImage imageWithContentsOfFile:filepath];
    }else if( indexPath.section == 3){
        cell.textLabel.text = CNameList[indexPath.row];
        NSString *filepath = [[ NSBundle mainBundle] pathForResource:@"1"ofType:@"jpg"];
        cell.imageView.image = [ UIImage imageWithContentsOfFile:filepath];
    }else{
        cell.textLabel.text = DNameList[indexPath.row];
        NSString *filepath = [[ NSBundle mainBundle] pathForResource:@"1"ofType:@"jpg"];
        cell.imageView.image = [ UIImage imageWithContentsOfFile:filepath];
    }
    // 设置列的按钮类型
    cell.accessoryType = UITableViewCellAccessoryNone;
    // 返回设置好数据的cell给UITableView对象
    return cell;
}

@end

/*
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;//选中哪一行
*/
/*
 - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;//针对可见的单元格进行调用

 监听uitableview数据加载完成重新设置tableview的高度。
 */
/*
- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath NS_AVAILABLE_IOS(6_0);
 */

/*
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;//设置行高,默认的高度是64
 */
//方法一:直接返回高度:eg.、
/*
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60.0;
}
 */
//方法二:行高的自适应:eg、
/*
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIntegerPath*)NSIndexPath{
    CGFloat contentWidth = self.tableView.frame.size.width;
    //设置字体的大小
    UIFont *font = [UIFont systemFontOfSize:13];
    //该行要展示的内容
    NSString *contect = [data objectAtIndex:indexPath.row];
    //计算出提示完内容需要展示的大小
    CGSize size = [content sizeWithFont:font constrainedToSize:CGSizeMake(contentWidth, 1000) lineBreakMode:UILineBreakModeWordWrap];
    // 構建顯示行
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {//如果这个为空,就给他创建一个新的cell(组件)
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        /*new和alloc的区别是什么?
         new和alloc/init在功能上几乎是一致的,分配内存并完成初始化。差别在于,采用new的方式只能采用默认的初始化方法,而采用alloc方式可以用他定制的初始化方法。
         */
/*
    }
    CGRect rect = [cell.textLabel textRectForBounds:cell.textLabel.frame limitedToNumberOfLines:0];
    // 设置提示矩形的大小
    rect.size = size;
    // 重置列文本区域
    cell.textLabel.frame = rect;
    cell.textLabel.text = content;
    // 设置自动换行
    cell.textLabel.numberOfLines = 0;
    // 设置提示字体
    cell.textLabel.font = font;
    return cell;
}
*/

四、通讯录的展示:

猜你喜欢

转载自blog.csdn.net/minld/article/details/79896649
今日推荐