您的位置:首页 > 产品设计 > UI/UE

关于UITableView基本用法

2015-02-25 16:36 375 查看
在iOS中,UITableView是常用的控件,多用于数据的显示,可自定义cell,功能强大且比较常用。

UITableView是一个可以滚动继承于UIScrollView的控件.类似于通讯录这种就是用这个来实现的。还有一些具有相同类似结构的都可以用UITableView去实现。UITableView只有两个样式。

UITableView实现有必须实现的两个协议方法,两个代理UITableViewDataSource,UITableViewDelegate

下面就使用UITableView实现一个简单实例:

使用UITableView实现通讯录:

Student.h

#import <Foundation/Foundation.h>

@interface Student :NSObject

@property (nonatomic,retain)NSString *name;

@property (nonatomic,retain)NSString *age;

@property (nonatomic,retain)NSString *sex;

@property (nonatomic,retain)NSString *phoneNumber;

//初始化方法
- (id)initWithDictionary:(NSDictionary *)dic;

@end

Student.m

#import "Student.h"

@implementation Student

- (id)initWithDictionary:(NSDictionary *)dic
{
   self = [superinit];
   if (self) {

        [selfsetValuesForKeysWithDictionary:dic];
    }

    returnself;
}

//kvc的纠错方法,一旦发现字典中得key,类中没有对应的成员变量,就会被调用
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{

    
}
- (id)valueForUndefinedKey:(NSString *)key
{

    returnnil;
}
- (void)dealloc
{
    [_namerelease];
    [_sexrelease];

    [_phoneNumberrelease];
    [_agerelease];
    [superdealloc];
}

@end

MainViewController.m

#import "MainViewController.h"

//引入头文件

#import "Student.h"

@interfaceMainViewController ()<UITableViewDataSource,UITableViewDelegate>

//section标题数组

@property (nonatomic,retain)NSMutableArray *arr;

//字典的

@property (nonatomic,retain)NSMutableDictionary *sourceDic;

@end

@implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle
*)nibBundleOrNil
{
   self = [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];
   if (self) {
        [selfhandleData];
    }

    returnself;
}

//写一个方法:专门处理数据
- (void)handleData
{

    NSString *path = [[NSBundlemainBundle]pathForResource:@"Class21Contact"ofType:@"plist"];

    NSDictionary *dataDic = [NSDictionarydictionaryWithContentsOfFile:path];

    

    //1.处理section标题数组

    //通过allkeys方法获取字典的所有key
   self.arr =[NSMutableArrayarrayWithArray:[dataDicallKeys]];

    

   //对标题进行排序
    [self.arrsortUsingSelector:@selector(compare:)];
   NSLog(@"%@",self.arr);

    

   //2.处理所有的数据,将每个学生的字典转化为学生的对象

    //初始化字典

    self.sourceDic = [NSMutableDictionarydictionary];

    

   //遍历原始的字典数据
   for (NSString *keyin dataDic) {

        

       //取相应的数组
       NSArray *tempArr = [dataDicobjectForKey:key];

        

        //创建一个新的数组,用来装student对象

        NSMutableArray *stuArr = [NSMutableArrayarray];

        
       for (NSDictionary *tempDicin tempArr) {

            //将每个小字典都转为一个Student对象
           Student *stu = [[Studentalloc]initWithDictionary:tempDic];
            [stuArraddObject:stu];
            [sturelease];

            
        }

        //将新的数组按照原来的key赋值给新的字典属性(sourceDic)
        [self.sourceDicsetObject:stuArrforKey:key];
    }

    
}

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    

    self.view .backgroundColor = [UIColorwhiteColor];

    self.navigationItem.title =@"通讯录";

    

    

    UITableView *tableView = [[UITableViewalloc]initWithFrame:CGRectMake(0,0,375,667-64)style:UITableViewStyleGrouped
];

    
    tableView.separatorColor = [UIColorgreenColor];
    tableView.rowHeight =50;

    
    [self.viewaddSubview:tableView];
    tableView.dataSource =self;
    tableView.delegate =self;
    [tableViewrelease];

    

    

    

    

    
}

//section数量
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    returnself.arr.count;
}

//section的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
   return [self.arrobjectAtIndex:section];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    //1.取得section标题对应的Key
   NSString *key = [self.arrobjectAtIndex:section];

    

    //2.取得key对应的数组
   NSArray *tempArr  = [self.sourceDicobjectForKey:key];

    

   //3.返回取得的数组的元素的个数
   return tempArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath
{
   staticNSString *str =@"reuse";

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:str];
   if (cell ==nil) {

        cell = [[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:str]autorelease];
    }

    //cell.textLabel.text = [self.arr objectAtIndex:indexPath.row];

    

    //1.通过indexPath的 section属性,去section标题数组中取得Key
   NSString *key = [self.arrobjectAtIndex:indexPath.section];

    

    //2.通过key取得对应的数组
   NSArray *tmpArr = [self.sourceDicobjectForKey:key];

    

    //3.通过indexPath的row属性,取得tmpArr中相应的student对象
   Student *stu = [tmpArrobjectAtIndex:indexPath.row];

    

    //4.赋值
    cell.textLabel.text = stu.name;

    cell.detailTextLabel.text = stu.phoneNumber;

    
   return cell;
}

- (void)dealloc
{
    [_arrrelease];
    [superdealloc];
}

//这里用的plist文件

最后展示结果:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息