iOS QQ列表效果实现

效果如下:

实现效果主要分为两个部分:

  1. 数据模型
  2. tableview

要实现tableview需要实现DataSource和delegate

datasource主要作用在于显示什么数据

delegate主要作用是事件响应即处理

代码如下:.h

// controller
@interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *dataList;   // 数据
@property (nonatomic, retain) UITableView *tableView;

@end

.m 文件

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self initDataList];   // 初始化数据
    
    // 向下移20
    CGRect frame = self.view.frame;
    frame.origin.y = 20;
    frame.size.height = frame.size.height - 20;
    self.tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
    
    // 设置委托
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - DataSource实现
// 一共几个section
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.dataList count];
}

// 每个section下有多少个组
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    List *list = [self.dataList objectAtIndex:section];
    NSMutableArray *array = list.groups;
    NSInteger all = [array count];
    for (Group *group in array) {
        if (group.isDelop) {
            all = all + [group.friends count];
        }
    }
    return all;
}

// 显示每个cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self isGroup:indexPath]) {
        return [self getGroupCell:tableView and:indexPath];   // 分组的cell
    }
    return [self getFriendCell:tableView and:indexPath];      // 好友cell
}

/*
 * 获得section标题
 **/
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    List *list = [self.dataList objectAtIndex:section];
    return list.title;
}

#pragma mark - 点击事件
// 单击的处理,若是分组 则展开,否则不处理
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    Group *group = [self isGroup:indexPath];
    // 如果点击的是分组,则取当前状态的反并更新数据
    if (group) {
        group.isDelop = !group.isDelop;
        [self.tableView reloadData];
    }
}

#pragma mark - 获取点击位置的数据
/*
 * 所点位置是否是分组,返回所点分组数据
 **/
- (Group *)isGroup:(NSIndexPath *)indexPath {
    int num = -1;
    NSUInteger index = indexPath.row;
    
    List *list = [_dataList objectAtIndex:indexPath.section];
    NSMutableArray *groups = list.groups;
    for (Group *group in groups) {
        num++;
        if (index == num) {
            return group;
        }
        if (group.isDelop) {
            num += [group.friends count];
            if (index <= num) {
                return nil;
            }
        }
    }
    return nil;
}

/*
 * 所点位置是否是好友,并返回所点好友数据
 **/
- (Friend *)isFriend:(NSIndexPath *)indexPath {
    int num = -1;
    NSUInteger index = indexPath.row;
    
    List *list = [_dataList objectAtIndex:indexPath.section];  // 获取点的section
    NSMutableArray *groups = list.groups;                      // 获取section下的所有分组
    for (Group *group in groups) {
        num++;
        if (group.isDelop) {
            int temp = num;
            num += [group.friends count];
            if (index <= num) {
                int k = index - temp - 1;
                return [group.friends objectAtIndex:k];
            }
        }
    }
    return nil;
}

#pragma mark - 初始化cell
/*
 * 设置分组的cell
 **/
- (UITableViewCell *)getGroupCell:(UITableView *)tableView and:(NSIndexPath *)indexPath {
    Group *nowGroup = [self isGroup:indexPath];
    static NSString *CellWithIdentifier = @"GroupCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellWithIdentifier];
        cell.backgroundColor = [UIColor colorWithRed:227/250.0 green:168/250.0 blue:105/250.0 alpha:1];
    }
    
    cell.textLabel.text = nowGroup.title;
    cell.detailTextLabel.text = nowGroup.detail;
    if (nowGroup.isDelop) {
        cell.imageView.image = [UIImage imageNamed:@"collect.png"];
    }
    else {
        cell.imageView.image = [UIImage imageNamed:@"tomore.png"];
    }
    return cell;
}

/*
 * 设置好友cell
 **/
- (UITableViewCell *)getFriendCell:(UITableView *)tableView and:(NSIndexPath *)indexPath {
    Friend *nowFriend = [self isFriend:indexPath];
    static NSString *CellWithIdentifier = @"FriendCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellWithIdentifier];
    }
    // 设置cell数据
    cell.textLabel.text = nowFriend.name;
    cell.detailTextLabel.text = nowFriend.state;
    cell.imageView.image = [UIImage imageNamed:nowFriend.image];
    return cell;
}

#pragma mark - 大小样式设置
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self isGroup:indexPath]) {
        return 30;   // 分组高
    }
    return 40;       // 好友的行高
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 20;
}

#pragma mark - 所显示的数据
/*
 * 数据初始化构造
 **/
- (void)initDataList {
    Friend *friend1 = [[Friend alloc] initWithImage:@"back1.jpg" name:@"苦海无涯" states:@"[在线] 哎 心疼!!"];
    Friend *friend2 = [[Friend alloc] initWithImage:@"back2.jpg" name:@"段天涯" states:@"[在线] 心疼!!"];
    Friend *friend3 = [[Friend alloc] initWithImage:@"back3.jpg" name:@"小妹妹" states:@"[在线] 疼!!"];
    Friend *friend4 = [[Friend alloc] initWithImage:@"back4.jpg" name:@"呼哈" states:@"[在线]心疼!!"];
    
    NSMutableArray *array1 = [NSMutableArray arrayWithObjects:friend1, friend2, friend3, friend4, nil];
    Group *group1 = [[Group alloc] initWithTitle:@"家人" delop:NO friends:array1];
    group1.detail = @"4/4";
    
    Friend *friend5 = [[Friend alloc] initWithImage:@"back5.jpg" name:@"段" states:@"[在线] 心疼!!"];
    Friend *friend6 = [[Friend alloc] initWithImage:@"back6.jpg" name:@"小商人" states:@"[在线] 疼!!"];
    Friend *friend7 = [[Friend alloc] initWithImage:@"back7.jpg" name:@"大哥" states:@"[离线] 心疼!!"];
    
    NSMutableArray *array2 = [NSMutableArray arrayWithObjects:friend5, friend6, friend7, nil];
    Group *group2 = [[Group alloc] initWithTitle:@"朋友" delop:NO friends:array2];
    group2.detail = @"2/3";
    
    NSMutableArray *l1 = [NSMutableArray arrayWithObjects:group1, group2, nil];
    List *list1 = [[List alloc] initWithTitle:@"我的好友" groups:l1];
    
    Friend *mobile = [[Friend alloc] initWithImage:@"back1.jpg" name:@"我的手机" states:@"[离线]手机未上线"];
    NSMutableArray *array = [NSMutableArray arrayWithObjects:mobile, nil];
    Group *group = [[Group alloc] initWithTitle:@"我的设备" delop:NO friends:array];
    group.detail = @"0/1";
    
    NSMutableArray *l2 = [NSMutableArray arrayWithObjects:group, nil];
    List *list2 = [[List alloc] initWithTitle:@"我的手机" groups:l2];
    
    self.dataList = [NSMutableArray arrayWithObjects:list2, list1, nil];
}

@end

========================================

数据模型实现

// 好友数据对象
@interface Friend : NSObject

@property (nonatomic, retain) NSString *image;  // 头像
@property (nonatomic, retain) NSString *name;   // 昵称
@property (nonatomic, retain) NSString *state;  // 说说

@end

// 分组对象
@interface Group : NSObject

@property (nonatomic, assign) BOOL isDelop;             // 是否展开
@property (nonatomic, retain) NSString *title;          // 分组名称
@property (nonatomic, retain) NSString *detail;         // 在线状态
@property (nonatomic, retain) NSMutableArray *friends;  // 好友列表

@end

// 整个qq列表
@interface List : NSObject

@property (nonatomic, retain) NSString *title;          // 列表名称
@property (nonatomic, retain) NSMutableArray *groups;   // 列表内分组

@end

数据模型的构造方法就不贴上来了,实现部分每个类只有一个构造方法

时间: 2024-08-28 04:56:25

iOS QQ列表效果实现的相关文章

iOS效果图---------------------qq粘性效果

这几天做了一些简单iOS的效果图,感觉苹果官方已经帮我们做了很多了,我们只是站在巨人的肩膀上编程,这些也没什么难的,最难的也就是用到了初中的三角函数,先让大家看看这几个动画吧.先列这几个把,由上而下分别是 数据缓冲效果 ,粒子动画,HUD指示效果,QQ未读消息的粘性效果,图一把一半遮住就是一种音乐播放器的播放效果,好了图一,图二, 图三都好简单就好似黄子华讲过,我只要一张嘴,两只手就可以把她搞得好嗨皮,而图一 图二,图三就是只要一个Animation,两个Layer就搞定了,而图四也不难,下面详

iOS tableViewCell 在cell赋值、网络加载照片位置偏移大小错乱,做一个类似qq列表的tableview

需求: 类似QQ列表的头像加载刷新,判断在线离线状态改变头像,以及彩色头像灰色处理,下载图片+获取在线状态需要连网--再改变头像 问题:由于cell的复用以及下拉刷新数据每次加载10条数据,会出现头像赋值不正确,位置偏移大小不同的变化 原因:由于cell的重复调用,加载数据方法已经赋值方法也在重复的调用,所以头像加载 在线状态判断好后,网络延迟, (个人开始yy:启动时的cell和赋值结束的cell可能不是同一个) 修改:当cell开始调用的时候,给当前的cell赋tag值,加载结束判断是不是自

ios QQ下拉列表 UITableViewHeaderFooterView

QQ下拉列表,最近找了一下网上没有类似的例子,今天做了一个Demo 图片可以在评论里留言留下邮箱, #import "ViewController.h" @interface ViewController ()<UITableViewDelegate,UITableViewDataSource> @property (nonatomic,strong)NSMutableArray * dataArray; @property (nonatomic,weak)UITableV

实现ios常见菜单效果的思路

目前见过的实现边侧菜单的效果,比较流行的有以下三种:(效果图) 1.菜单栏覆盖在部分主视图上 附上实现该效果的一个不错的源码地址: http://code4app.com/ios/RNFrostedSidebar/524399706803fa3c33000001 (1)最开始要实现这个效果,我想最简单的方式就是:添加UIView,加上一个self.view大小的子视图,菜单列表以外的区域设为透明灰色.后来发现,如果当前的控制器有显示导航栏或者工具栏,这个子视图就无法遮盖住导航栏或者工具栏上面的按

使用ExpandableListView做一个类似QQ列表效果图

分组列表视图(ExpandableListView) 和ListView不同的是它是一个两级的滚动列表视图,每一个组可以展开,显示一些子项,类似于QQ列表,这些项目来至于ExpandableListAdapter的子类,也就是说,要实现向里面添加项目,必须写一个子类实现ExpandableListAdapter的接口或者使用系统为我们实现在子类 常用属性 1. android:childDivider 指定各组内子类表项之间的分隔条, 2. android:childIndicator 显示在子

iOS星级评分效果实现

原文链接: iOS星级评分效果实现 简书主页:http://www.jianshu.com/users/37f2920f6848 Github主页:https://github.com/MajorLMJ iOS开发者公会-技术1群 QQ群号:87440292 iOS开发者公会-技术2群 QQ群号:232702419 iOS开发者公会-议事区   QQ群号:413102158

iOS文字滚动效果 之纵向滚动

原文链接: iOS文字滚动效果 之纵向滚动 简书主页:http://www.jianshu.com/users/37f2920f6848 Github主页:https://github.com/MajorLMJ iOS开发者公会-技术1群 QQ群号:87440292 iOS开发者公会-技术2群 QQ群号:232702419 iOS开发者公会-议事区   QQ群号:413102158

iOS文字滚动效果 之横向滚动

原文链接: iOS文字滚动效果 之横向滚动 简书主页:http://www.jianshu.com/users/37f2920f6848 Github主页:https://github.com/MajorLMJ iOS开发者公会-技术1群 QQ群号:87440292 iOS开发者公会-技术2群 QQ群号:232702419 iOS开发者公会-议事区   QQ群号:413102158

某网站品牌的列表效果(引自锋利的jQuery)

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta name="generator" content="editplus" /> <title>某网站品牌的列表效果</title> <script src="jquery-1.7.1.min.js" type