iOS开发技巧iOS 开发

UITableView及简单通讯录功能

2015-12-03  本文已影响254人  ThEAll

import "AppDelegate.h"

import "RootViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


import "RootViewController.h"

@interface RootViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic ,retain)NSArray *dataArray; // 数据源,用来给cell赋值
@property (nonatomic ,retain)NSDictionary *dict;
@property (nonatomic ,retain)NSMutableArray *titleArray; // 用来盛放头标题

@end

@implementation RootViewController

pragma mark --- 表视图的代理方法

// 共有多少个分区 此代理方法为可选的,如果不实现该代理方法,默认整个表视图只有一个分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
// return 1;
// 返回字典中元素的个数,作为分区的个数
return self.dict.count;
}

// 每个分区下返回的行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// return 10;
// 这里单元格个数一般不写死,将数据源的个数作为返回值,根据数据的数量创建单元格的数量
// return self.dataArray.count;// 返回所有数据的个数
// 根据当前所在的分区,取得字典中对应的键
// NSString *keyString = self.dict.allKeys[section];
// // 根据键取出对应的值,由于字典的值为数组,所以可以有count属性
// return [self.dict[keyString] count];

//  根据所在分区取出字典的值
NSArray *array = [self.dict.allValues objectAtIndex:section];
return array.count;

}

// 定义单元格 IndexPath:单元格当前所在位置
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *identifier = @"cell";
//  identifier: 因为一个表视图中可能存在多种样式的单元格,我们把相同样式的单元格放到同一个集合里面,为这个集合加标示符,当我们需要用到某种样式的单元格的时候,根据不同的标示符,从不同的集合中找寻单元格
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];//  在某个标示符下,可以再度被移出重新使用的cell

// � 如果从集合中为找到单元格,也就是集合中好没有单元格,也就是还没有单元格出屏幕,那么我们需要创建单元格
if (!cell) {
// 创建cell的时候需要标示符是因为,当该cell出屏幕的时候将单元格按照不同类型放入和集中
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];

    //  设置cell的Style,不涉及到数据的重新赋值,我们可以在初始化cell的时候给它设置好
    //  设置辅助视图
    cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
    //  设置选中后的效果
    cell.selectionStyle = UITableViewCellSelectionStyleDefault;
}

//  创建单元格

// UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];

//  为cell上添加文字      indexPath.row : 单元格所在的行数

// cell.textLabel.text = [NSString stringWithFormat:@"我是第%ld个单元格,我在%ld分区",(long)indexPath.row+1,indexPath.section];
// indexPath.row是得到当前单元格所在的那一行,起始位置为0,数组下标的起始位置也是零,所以我们可以根据单元格所在的行数来从数组中取值显示
// cell.textLabel.text = [self.dataArray objectAtIndex:indexPath.row];

// NSString *keyString = self.dict.allKeys[indexPath.section];
// NSArray *array = self.dict[keyString];
NSArray *valueArray = [self.dict.allValues objectAtIndex:indexPath.section];

cell.textLabel.text = [valueArray objectAtIndex:indexPath.row];

//  副标题  这种样式只能在非default的样式下使用,只为显示位置有所改变
cell.detailTextLabel.text = @"副标题,颜色浅色";
//  不管任何Style样式下,都给可以给cell添加图片
cell.imageView.image = [UIImage imageNamed:@"11"];

return cell;
}

// 为每个分区头添加标题的代理方法
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
// 为每个分区添加头标题
// NSArray *titArray = [NSArray arrayWithObjects:@"1",@"2", nil];
// return [titArray objectAtIndex:section];

// NSString *string = [[self.dict.allValues objectAtIndex:section] objectAtIndex:0];
// NSString *subStr = [string substringToIndex:1];
// return subStr;

int index = (int)(self.titleArray.count > section ? section : -1);
if (index != -1) {
    return [self.titleArray objectAtIndex:section];
}else
    return @"数组元素不够了";

}

//� 点击cell所响应的代理方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
// 根据indexPath得到当前所点击的cell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"------%ld",(long)indexPath.row);
}

// 通过代理设置cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

//  一般cell都需要根据内容来自适应高度,高度的变化就在此处根据indexPath来更改(每个cell的高度不统一)
//  设置第一个cell高度200,其他都是100
if (indexPath.row == 0) {
    return 100;
}
return 60;

}

// 添加右侧索引条的代理方法
-(NSArray<NSString *> *)sectionIndexTitlesForTableView:(UITableView *)tableView{
return [NSArray arrayWithObjects:@"a",@"b",@"c",@"d", nil];
}

上一篇 下一篇

猜你喜欢

热点阅读