您的位置:首页 > 其它

05---MVC模式下动态调整Cell高度三部曲

2014-07-24 13:53 344 查看

动态调整Cell高度三部曲

  我们在做项目开发的过程中经常会遇到每一个cell的高度及cell的子控件的显示个数不同,以我最近开发的微格为例,讲解一下MVC模式动态的调整Cell宽高的三部曲

  MVC模式一般都是 1.创建一个Model类 2.创建一个View类(View类含有Model属性) 3.在Controller类里面 实例化一个Model 实例化一个View 将实例化的Model赋值給View来显示数据。

//
//  HomeViewController.m
//  新浪微博控
//
//  Created by whblap on 14-6-25.
//  Copyright (c) 2014年 whblap. All rights reserved.
//

#import "HomeViewController.h"
#import "UIBarButtonItem+WHBALP.h"
#import "HttpTool.h"
#import "Status.h"
#import "User.h"
#import "StatusManage.h"
#import "StatusCell.h"
#import "StatusCellFrame.h"

@interface HomeViewController ()
{
NSMutableArray *_statuses; // 所有的微博数据
}
@end

@implementation HomeViewController

- (void)viewDidLoad
{
[super viewDidLoad];

[self buildNavigationBar];
[self loadStatusData];
}

#pragma mark - build导航栏
- (void)buildNavigationBar
{
self.title = @"首页";
self.view.backgroundColor = [UIColor cyanColor];

// 添加给导航栏左图片按钮
self.navigationItem.leftBarButtonItem = [UIBarButtonItem barButtonItemWithImage:@"navigationbar_compose.png" highlightedImage:@"navigationbar_compose_highlighted.png" addTarget:self action:@selector(sendStatus)];

// 给导航栏添加右图片按钮
self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemWithImage:@"navigationbar_pop.png" highlightedImage:@"navigationbar_pop_highlighted.png" addTarget:self action:@selector(popMenu)];
}
#pragma mark - 加载微博数据
- (void)loadStatusData
{
_statuses = [NSMutableArray array];
//    [HttpTool getWithPath:@"2/statuses/home_timeline.json" params:nil success:^(id JSON) {
//        NSArray *statuses = JSON[@"statuses"]; // JSON返回的数据默认是20条 为数组类型的对象
//        // 将字典模型转化为模型对象(将模型对象添加到statuses微博数组中)
//        for (NSDictionary *dict  in statuses) {
//            //
//            Status *status = [[Status alloc] initWithDict:dict];
//             [_statuses addObject:status];
//        }
//        NSLog(@"_____________%d",_statuses.count);
//         [self.tableView reloadData];
//    } failure:^(NSError *error) {
//        NSLog(@"%@",error);
//    }];
// 微博管理 加载
[StatusManage getStatusesWithSuccess:^(NSArray *statues) {
[_statuses addObjectsFromArray:statues];
[self.tableView reloadData];
} failure:^(NSError *error) {
NSLog(@"%@",error);
}];
}

#pragma mark - 发送微博方法
- (void)sendStatus
{
MyLog(@"调用了发送微博方法");

}

#pragma mark - 弹出菜单
- (void)popMenu
{
MyLog(@"调用了弹出菜单方法");
}

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

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
NSLog(@"++++++++++++++++%d",_statuses.count);
return _statuses.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
StatusCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];

if (cell == nil) {
// 创建一个自定义的cell
cell = [[StatusCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
// 实例化一个微博frame对象
StatusCellFrame *sCellFrame = [[StatusCellFrame alloc] init];
// 将访问服务器得到的微博数据赋值给微博frame对象的status模型 需要根据这个设置frame
sCellFrame.status = _statuses[indexPath.row];
[cell setStatusCellFrame:sCellFrame];

return cell;
}

#pragma mark 返回每一行cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
StatusCellFrame *f = [[StatusCellFrame alloc] init];
f.status = _statuses[indexPath.row];
return f.cellHeight;
}
@end


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