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

iOS UI 08 uitableview 自定义cell

2015-12-13 16:08 337 查看
//

// RootViewController.m

// ui - 09 通讯录

//

// Created by dllo on 15/11/19.

// Copyright (c) 2015年 dllo. All rights reserved.

//

#import "RootViewController.h"

#import "Contact.h"

@interface
RootViewController () <UITableViewDelegate ,
UITableViewDataSource>

@property (nonatomic,
retain) NSMutableDictionary *contactDic;

@property (nonatomic,
retain) UITableView *tablev;

@end

@implementation RootViewController

- (void)dealloc

{

[_contactDic
release];

[super dealloc];

}

- (void)viewDidLoad {

[super
viewDidLoad];

self.view.backgroundColor = [UIColor
whiteColor];

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

[self getData];

self.tablev = [[UITableView
alloc]initWithFrame:self.view.frame
style:UITableViewStylePlain];

self.tablev.delegate =
self;

self.tablev.dataSource =
self;

[self.view
addSubview:self.tablev];

[_tablev
release];

#warning 编辑 -
添加编辑键

self.navigationItem.rightBarButtonItem =
self.editButtonItem;

// Do any additional setup after loading the view.

}

#warning 编辑2 -
让tableview处于编辑状态

- (void)setEditing:(BOOL)editing animated:(BOOL)animated

{

[super setEditing:editing
animated:animated];

//当点击编辑的时候table也进入编辑状态

[self.tablev
setEditing:editing
animated:animated];

}

#warning 编辑3
指定tableview哪些行可以编辑

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath

{

return
YES;

}

#warning 编辑4 -
指定tableview编辑的样式(添加,
删除)

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

{

if (indexPath.section < 4) {

return
UITableViewCellEditingStyleDelete;

} else {

return
UITableViewCellEditingStyleInsert;

}

}

#warning 编辑5
编辑完成(先操作数据源,再修改UI)

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath
*)indexPath

{

NSString *key = [self.contactDic.allKeys
objectAtIndex:indexPath.section];

NSMutableArray *Arr = [self.contactDic
objectForKey:key];

//删除操作

if (UITableViewCellEditingStyleDelete == editingStyle) {

//先删数据

[Arr removeObjectAtIndex:indexPath.row];

//再改UI

//局部修改 -
只修改指定行

// [self.tablev deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];

//全局修改 -
重新加载一遍

[self.tablev
reloadData];

}

if (UITableViewCellEditingStyleInsert == editingStyle) {

//先改数据

Contact *cta = [[Contact
alloc]init];

cta.name =
@"王宁";

cta.gender =
@"男";

cta.phoneNumber =
@"54765735";

cta.age =
@"23";

cta.hobby=
@"qingchung";

[Arr insertObject:cta
atIndex:indexPath.row];

[cta release];

//再改ui

[self.tablev
insertRowsAtIndexPaths:[NSArray
arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];

}

}

#warning 移动1 -参照编辑的添加编辑键

#warning 移动 2 -
参照编辑的让tableview处于编辑状态

#warning 移动3 -
指定table哪些行可以移动

- (bool)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath

{

return
YES;

}

#warning 移动4
移动完成 (移动功能的逻辑)

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath
*)destinationIndexPath

{

NSString *key = [self.contactDic.allKeys
objectAtIndex:sourceIndexPath.section];

NSMutableArray *Arr = [self.contactDic
objectForKey:key];

//根据原地址找到待操作的contact对象

//注意,
手动进行引用计数加1的操作,
防止从数组数组移除后因为计数减1引起空间释放

Contact *contact = [[Arr
objectAtIndex:sourceIndexPath.row]
retain];

[Arr removeObjectAtIndex:sourceIndexPath.row];

//找到移动的目的地

NSString *key2 = [self.contactDic.allKeys
objectAtIndex:destinationIndexPath.section];

NSMutableArray *Arr2 = [self.contactDic
objectForKey:key2];

[Arr2 insertObject:contact
atIndex:destinationIndexPath.row];

//不要忘记release
对应手动retain

[contact release];

//再改ui -
注意应用局部改变时要注意原来的位置是否还有数据

// [self.tablev moveRowAtIndexPath:sourceIndexPath toIndexPath:destinationIndexPath]

[self.tablev
reloadData];

}

#warning 移动5
检测移动过程 实现限制跨区移动

- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath
*)proposedDestinationIndexPath

{

//若目的区与原区不同,则返回原地址不允许移动

if (sourceIndexPath.section != proposedDestinationIndexPath.section) {

return sourceIndexPath;

} else {

return proposedDestinationIndexPath;

}

}

- (void)getData

{

self.contactDic = [NSMutableDictionary
dictionary];

NSString *filepath = [[NSBundle
mainBundle] pathForResource:@"contact"
ofType:@"plist"];

NSMutableDictionary *dic = [NSMutableDictionary
dictionaryWithContentsOfFile:filepath];

for (NSString *key
in dic) {

NSMutableArray *Arr = [NSMutableArray
array];

for (NSMutableDictionary *temp
in [dic objectForKey:key]) {

Contact *contact = [[Contact
alloc]init];

// contact.name = [temp objectForKey:@"name"];

// contact.gender = [temp objectForKey:@"gender"];

// contact.age = [temp objectForKey:@"age"];

// contact.phoneNumber = [temp objectForKey:@"phoneNumber"];

// contact.hobby = [temp objectForKey:@"hobby"];

// contact.picture = [temp objectForKey:@"picture"];

//上面的这这一大段可以用一句话代替

[contact setValuesForKeysWithDictionary:temp];

[Arr addObject:contact];

[contact release];

}

[self.contactDic
setObject:Arr forKey:key];

}

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

return 100;

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section

{

return [self.contactDic.allKeys
objectAtIndex:section];

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return
self.contactDic.allKeys.count;

}

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

{

NSString *key = [self.contactDic.allKeys objectAtIndex:section];

NSMutableArray *Arr = [self.contactDic objectForKey:key];

return Arr.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

NSString *cellstr = @"cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellstr];

if (nil == cell) {

cell = [[[UITableViewCell alloc]initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier:cellstr] autorelease];

}

NSString *key = [self.contactDic.allKeys objectAtIndex:indexPath.section];

NSMutableArray *arr = [self.contactDic objectForKey:key];

Contact *contact = [arr objectAtIndex:indexPath.row];

cell.selectionStyle = UITableViewCellSelectionStyleNone;

cell.textLabel.text = contact.name;

cell.detailTextLabel.text = contact.phoneNumber;

return cell;

}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView

{

return self.contactDic.allKeys;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

NSString *key = [self.contactDic.allKeys objectAtIndex:indexPath.section];

NSMutableArray *Arr = [self.contactDic objectForKey:key];

Contact *contact = [Arr objectAtIndex:indexPath.row];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/

@end

//

// GirlTableViewCell.m

// ui- 10 自定义cell

//

// Created by dllo on 15/11/20.

// Copyright (c) 2015年 dllo. All rights reserved.

//

#import "GirlTableViewCell.h"

@implementation GirlTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

self = [super
initWithStyle:style
reuseIdentifier:reuseIdentifier];

if (self) {

[self
createSubview];

}

return
self;

}

- (void)createSubview

{

self.backgroundColor = [UIColor
yellowColor];

#warning label的位置不要用self.contentview.. .来确定

// UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];

UILabel *label1 = [[UILabel
alloc]initWithFrame:CGRectMake([[UIScreen
mainScreen] bounds].size.width - 50,0 ,50, 50)];

label1.text =
@"余浩";

label1.backgroundColor = [UIColor
blueColor];

self.contentView.backgroundColor = [UIColor
orangeColor];

//cell有3个区
编辑区,
辅助区, 内容区 contentview

[self.contentView
addSubview:label1];

// [self addSubview:label1];

[label1 release];

}

- (void)awakeFromNib {

// Initialization code

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

[super setSelected:selected
animated:animated];

// Configure the view for the selected state

}

@end

//

// BoyTableViewCell.m

// ui- 10 自定义cell

//

// Created by dllo on 15/11/20.

// Copyright (c) 2015年 dllo. All rights reserved.

//

#import "BoyTableViewCell.h"

@implementation BoyTableViewCell

+ (CGFloat)heightWithstr:(NSString *)str

{

// CGRect frame = [self.label1.text boundingRectWithSize:CGSizeMake(300, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:18.0] forKey:NSFontAttributeName] context:nil];

CGRect frame = [str
boundingRectWithSize:CGSizeMake(300, 1000)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:[NSDictionary
dictionaryWithObject:[UIFont
systemFontOfSize:18.0] forKey:NSFontAttributeName]
context:nil];

return frame.size.height;

}

-(void)layoutSubviews

{

[super
layoutSubviews];

#warning 自适应高度2 -
自适应label的高度,
注意label高度应于自适应指定的高度相同

// CGRect frame = [self.label1.text boundingRectWithSize:CGSizeMake(300, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:18.0] forKey:NSFontAttributeName] context:nil];

//当.语法
与访问结构体成员的.混合时
系统不识别会报错

// self.label1.frame.size.height = frame.size.height;

CGFloat height = [[self
class] heightWithstr:self.label1.text];

// CGFloat height = [self heightWithstr:self.label1.text];

//先保存label的坐标及宽度信息

CGRect framel =
self.label1.frame;

// framel.size.height = frame.size.height;

framel.size.height = height;

self.label1.frame = framel;

}

/***********************************上面第二种方法************************/

//- (void)setContent:(NSString *)content

//{

// if (content != _content) {

// [_content release];

// _label1 = [content copy];

// }

// self.label1.text = content;

//

// CGRect frame = [content boundingRectWithSize:CGSizeMake(300, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:18.0] forKey:NSFontAttributeName] context:nil];

// //当.语法
与访问结构体成员的.混合时
系统不识别会报错

// // self.label1.frame.size.height = frame.size.height;

// //先保存label的坐标及宽度信息

// CGRect framel = self.label1.frame;

// framel.size.height = frame.size.height;

// self.label1.frame = framel;

//

//}

/*****************************************************************************/

- (void)dealloc

{

[_label1
release];

[super dealloc];

}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

self = [super
initWithStyle:style
reuseIdentifier:reuseIdentifier];

if (self) {

[self
createSubview];

}

return
self;

}

- (void)createSubview

{

self.backgroundColor = [UIColor
yellowColor];

#warning label的位置不要用self.contentview.. .来确定

// UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];

self.label1 = [[UILabel
alloc]initWithFrame:CGRectMake(0, 0, 300, 50)];

self.label1.text =
@"由盛焱";

#warning 自适应高度3 -
显示行数设置为自适应

self.label1.numberOfLines = 0;

// [self addSubview:label1];

[_label1
release];

}

//- (void)layoutSubviews

//{

// //当试图改变的时候会触发这个方法

// [super layoutSubviews];

//// NSLog(@"======= %f %f", self.contentView.frame.size.width,self.contentView.frame.size.height);

//}

- (void)awakeFromNib {

// Initialization code

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

[super setSelected:selected
animated:animated];

// Configure the view for the selected state

}

@end

//

// BoyTableViewCell.h

// ui- 10 自定义cell

//

// Created by dllo on 15/11/20.

// Copyright (c) 2015年 dllo. All rights reserved.

//

#import <UIKit/UIKit.h>

@interface BoyTableViewCell :
UITableViewCell

@property (nonatomic,retain)UILabel *label1;

@property (nonatomic,retain)NSString *content;

+ (CGFloat)heightWithstr:(NSString *)str;

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