iOS开发UI篇章使用UItableview完成一个简单的QQ好友列表(一)

iOS开发UI使用UItableview完成一个简单的QQ好友列表(一)

一、项目结构和plist文件

TXViewController.m


二、实现代码

1.说明:

主控制器直接继承UITableViewController

 

//

//  TXViewController.h

//  04-QQ好友列表

//

//  Created by on 14-10-13.

//  Copyright (c) 2014梁镋鑫. All rights reserved.

//

 

#import<UIKit/UIKit.h>

 

@interface TXViewController :UITableViewController

 

@end

storyboard中进行了关联

2.代码

数据模型部分:

TXFriendGroup.h文件

 

//  04-QQ好友列表

//

//  Created by on 14-10-13.

//  Copyright (c) 2014梁镋鑫. All rights reserved.

//

 

#import<Foundation/Foundation.h>

 

@interface TXFriendGroup :NSObject

@property (nonatomic,copy)NSString *name;

/**

 * 数组中装的都是TXFriend模型

 */

@property (nonatomic,strong)NSArray *friends;

@property (nonatomic,assign)int online;

 

+ (instancetype)groupWithDict:(NSDictionary *)dict;

- (instancetype)initWithDict:(NSDictionary *)dict;

 

 

@end

TXFriendGroup.m文件

//  04-QQ好友列表

//

//  Created by on 14-10-13.

//  Copyright (c) 2014梁镋鑫. All rights reserved.

//

 

#import"TXFriendGroup.h"

#import"TXFriend.h"

@implementation TXFriendGroup

+ (instancetype)groupWithDict:(NSDictionary *)dict

{

   return [[selfalloc]initWithDict:dict];

}

 

- (instancetype)initWithDict:(NSDictionary *)dict

{

   if (self = [superinit]) {

       // 1.注入所有属性将字典转换为模型

        [selfsetValuesForKeysWithDictionary:dict];

        

       // 2.特殊处理friends属性定义一个数组来保存转换后的模型

       NSMutableArray *friendArray = [NSMutableArrayarray];

       for(NSDictionary *dictinself.friends) {

           TXFriend *friend = [TXFriendfriendWithDict:dict];

            [friendArrayaddObject:friend];

        }

       self.friends = friendArray;

    }

   returnself;

}

 

@end

 

 

TXFriend.h文件

 

//  Created by on 14-10-13.

//  Copyright (c) 2014梁镋鑫. All rights reserved.

//

 

#import<Foundation/Foundation.h>

 

@interface TXFriend :NSObject

@property (nonatomic,copy)NSString *name;

@property (nonatomic,copy)NSString *icon;

@property (nonatomic,copy)NSString *intro;

@property (nonatomic,assign,getter = isVip)BOOL vip;

 

+ (instancetype)friendWithDict:(NSDictionary *)dict;

- (instancetype)initWithDict:(NSDictionary *)dict;

 

@end

TXFriend.m文件

 

//  Created by on 14-10-13.

//  Copyright (c) 2014梁镋鑫. All rights reserved.

//

 

#import"TXFriend.h"

 

@implementation TXFriend

+ (instancetype)friendWithDict:(NSDictionary *)dict

{

   return [[selfalloc]initWithDict:dict];

}

 

- (instancetype)initWithDict:(NSDictionary *)dict

{

   if (self = [superinit]) {

        [selfsetValuesForKeysWithDictionary:dict];

    }

   returnself;

}

视图部分

TXFriendCell.h文件

 

//  Created by on 14-10-14.

//  Copyright (c) 2014梁镋鑫. All rights reserved.

//

 

#import<UIKit/UIKit.h>

@classTXFriend;

@interface TXFriendCell :UITableViewCell

+ (instancetype)cellWithTableView:(UITableView*)tableView;

// friendC++的关键字,不能用friend作为属性名

@property (nonatomic,strong)TXFriend *friendData;

@end

 

 

TXFriendCell.m文件

 

//  Created by on 14-10-14.

//  Copyright (c) 2014梁镋鑫. All rights reserved.

//

 

#import"TXFriendCell.h"

#import"TXFriend.h"

@implementation TXFriendCell

 

+ (instancetype)cellWithTableView:(UITableView*)tableView

{

   staticNSString *ID =@"friend";

   TXFriendCell *cell = [tableViewdequeueReusableCellWithIdentifier:ID];

   if (cell ==nil){

       //这里使用系统自带的样式

        cell = [[TXFriendCellalloc]initWithStyle:UITableViewCellStyleSubtitlereuseIdentifier:ID];

    }

   return cell;

}

 

- (void)setFriendData:(TXFriend *)friendData

{

   _friendData = friendData;

    

   self.imageView.image = [UIImageimageNamed:friendData.icon];

   self.textLabel.text = friendData.name;

   self.detailTextLabel.text = friendData.intro;

}

 

@end

 

 

主控制器部分

TXViewController.m文件

#import"TXViewController.h"

#import"TXFriendGroup.h"

#import"TXFriend.h"

#import"TXHeaderView.h"

#import"TXFriendCell.h"

 

@interfaceTXViewController ()

@property (nonatomic,strong)NSArray *groups;

@end

 

@implementation TXViewController

 

- (void)viewDidLoad

{

    [superviewDidLoad];

    

   // 每一行cell的高度

   self.tableView.rowHeight = 50;

   // 每一组头部控件的高度

   self.tableView.sectionHeaderHeight = 44;

}

//1.先拿到数据,实现懒加载

- (NSArray *)groups

{

   if (_groups ==nil) {

       NSArray *dictArray = [NSArrayarrayWithContentsOfFile:[[NSBundlemainBundle]pathForResource:@"friends.plist"ofType:nil]];

        

       NSMutableArray *groupArray = [NSMutableArrayarray];

       for(NSDictionary *dictin dictArray) {

           TXFriendGroup *group = [TXFriendGroupgroupWithDict:dict];

            [groupArrayaddObject:group];

        }

        

       _groups = groupArray;

    }

   return_groups;

}

 

- (BOOL)prefersStatusBarHidden

{

   returnYES;

}

 

#pragma mark -数据源方法

//返回多少组

//为什么这里不会智能提示?因为这些方法是uitableview协议里的,默认并没有遵守协议,让主控制器类继承uitableviewcontroller后,就已经遵守了

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

   returnself.groups.count;

}

//每组每行的内容

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

{

   TXFriendGroup *group =self.groups[section];

   return group.friends.count;

}

 

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

{

   // 1.创建cell

   TXFriendCell *cell = [TXFriendCellcellWithTableView:tableView];

    

   // 2.设置cell的数据

   TXFriendGroup *group =self.groups[indexPath.section];

    cell.friendData = group.friends[indexPath.row];

    

   return cell;

}

 

/**

 * 返回每一组需要显示的头部标题(字符出纳)//当一个分组标题进入视野的时候就会调用该方法

 */

- (UIView *)tableView:(UITableView *)tableViewviewForHeaderInSection:(NSInteger)section

{

   // 1.创建头部控件

   TXHeaderView *header = [TXHeaderViewheaderViewWithTableView:tableView];

    

   // 2.header设置数据(header传递模型)

    header.group =self.groups[section];

    

   return header;

}

@end

实现的简陋效果:


三、注意点

1)设置头部视图的方法

- (UIView *)tableView:(UITableView *)tableViewviewForHeaderInSection:(NSInteger)section

{

   // 1.创建头部控件

   TXHeaderView *header = [TXHeaderViewheaderViewWithTableView:tableView];

    

   // 2.header设置数据(header传递模型)

    header.group =self.groups[section];

    

   return header;

}

//设置分组头部标题的高度

-(CGFloat)tableView:(UITableView *)tableViewheightForHeaderInSection:(NSInteger)section

{

   return44;

}

 

2)在重写set方法时,应该考虑到回滚。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值