关于UITableView基本用法

在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文件

最后展示结果:

时间: 2024-07-30 10:17:13

关于UITableView基本用法的相关文章

UITableView的用法--俩个UITableView并排

#import "ViewController.h" @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> {     UITableView*_tableView1;     UITableView*_tableView2; } @property(nonatomic,strong)NSMutableArray*dataArray1; @property(nonatomic,stro

UITableView类用法大全:UITableView属性

[storyboard创建tableView步骤] 1.设置根视图 2.选中视图,设置导航栏editor/embed in/navigationcontroller 3.cell设置Identifier标识 4.创建tableviewcontroller类,跟tableviewcontroller控件关联上 5.storyboard会自动遵守<</span>UITabBarControllerDelegate,UITableViewDataSource> 6.只需要实现必要的协议方

swift详解之十九--------------UITableView的基本操作(下拉刷新,新增删除,分组,检索等)

UITableView的基本操作(下拉刷新,新增删除,分组,检索等) 注:本小结总结UITableview的一些基本用法 UITbleView继承自UIScrollView,只能用来显示一列数据(目前就只认识到这里),纵向滑动. 一般有两种方式来实现,直接用UITableViewController , 占满整个屏幕 .不用手动实现UITableViewDataSource 和UITableViewDelegate .另一种方式在UIViewController 中.我们看看这种方式 let t

iOS开发 UITableView的方法和属性总结

本文描述UITableView的各种方法,属性,委托以及数据源.本文的目的只是总结UITableView的用法,详细的例子另撰文描述. 1 数据源  UITableViewDataSource协议 01 返回组(节)的个数,默认是返回1,如果只有1组数据,可以不用实现该方法. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 02 返回某一组的行数,该组由section的值决定 - (NSInteger)table

UICollectionView在Swift3.0中的用法

UICollectionView在Swift3.0中的用法 UICollectionView的初始化跟OC中是相似的,创建 GameView 集成自 UICollectionView .注意不同于UITableView的用法,他需要用 UICollectionViewFlowLayout 来指定一些需要设置的属性,或者可以通过遵守 UICollectionViewDelegateFlowLayout 这个代理来实现.下面我用设置属性的方式来实现的,比较方便. //布局 layout.scroll

iOS基础——通过案例学知识之UITableView(上)

iOS基础--通过案例学知识之UITableView(上) 对于UITableView的知识点特别多,因为它是iOS用得最多控件之一,我会尽我最大努力和语言的组织,将所有知识点介绍到位,今天要实现的效果图 吐槽 与Android对比,可以说跟ListView的实现几乎一样,跟RecyclerView一模一样 Android写起来似乎比iOS复杂一点,因为iOS大部分都被封装好了,这一点iOS做得好 对于iOS的方法的命名只能说又长又臭 知识点包括 UITableView的UITableViewD

抽取UITableView的DataSource代理方法及同一份View能接受不同模型数据

View controllers 通常是 iOS 项目中最大的文件,并且它们包含了许多不必要的代码.所以 View controllers 中的代码几乎总是复用率最低的.比如UITableView常规用法如下: 传统使用方法 1. 定义数据模型 @interface LFPhoto : NSObject @property (nonatomic,copy) NSString *name; @property (nonatomic,copy) NSString *title; @end 2. 设计

IOS开发之UITableView

UITableView就相当于android中的listview,在这里先介绍一个简单的UItableView的用法,因为我也是刚学.而且也遇到了一些问题.直接上代码吧 这是ViewController.h  #import <UIKit/UIKit.h> @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate> @property (nonatomic,retai

CollectionViewController 集合视图

 CollectionViewController 集合视图     UICollectionView, 继承于UIScollView, 可以滚动, 从iOS6才出现, 和UITableView的用法非常相似     tableView     dataSource: 显示数据     delegate: 样式和触发方法     collectionView     dataSource: 显示数据     delegate: 触发方法     UICollectionViewLayout: 样