码迷,mamicode.com
首页 > 其他好文 > 详细

1.定制section 2.实现类似QQ好友的折叠功能

时间:2015-09-28 20:47:55      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

/*
 ---定制段---
 1.定制导航栏
 2.创建数据元
 a .分配内存和初始化
 3.写两个dataSource的协议方法

//定制段头。段头中是一个按钮, 在按钮的响应事件里边,处理响应多少行,进而控制是否可以折叠。
  */
#import "MyTableViewController.h"
@interface MyTableViewController ()
{
    BOOL _sectionFlag[3];
}
@property (nonatomic) NSMutableArray *dataArray;
@end

@implementation MyTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self customNaviItam];
    [self createDataSource];
    //注册
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    //设置初始状态
    for(NSInteger i=0;i<3;i++){
        //NO表示折叠状态
        _sectionFlag[i] = NO;
    }
}

#pragma mark    ------------------------定制导航Item
- (void)customNaviItam
{
    self.navigationItem.title = @"QQ";
}

#pragma mark    ------------------------创建数据源
- (void)createDataSource
{
    //分配内存和初始化
    self.dataArray = [[NSMutableArray alloc] init];
    //创建第一个段
    NSArray *section1 = [[NSArray alloc] initWithObjects:@"冯洪涛", @"徐晓臣",@"朱鹏",@"王亨景",nil];
    //创建第二个段
    NSArray *section2 = [[NSArray alloc] initWithObjects:@"18",@"12",@"27", nil];
    //创建第三个段
    NSArray *section3 = [[NSArray alloc] initWithObjects:@"女",@"男",@"其他", nil];
    
    [self.dataArray addObject:section1];
    [self.dataArray addObject:section2];
    [self.dataArray addObject:section3];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    // Return the number of sections.
    return self.dataArray.count;
}

//设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    // Return the number of rows in the section.
    if(_sectionFlag[section] == NO){
        return NO;
    }
    return [self.dataArray[section] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //需要注册,在最上边
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.textLabel.text = self.dataArray[indexPath.section][indexPath.row];
    return cell;
}

//定制段头
#pragma mark    ------------------------custom section header
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSArray *textArray = @[@"我的好友",@"1510",@"qianfeng"];
    UIButton *btn = [[UIButton alloc] init];

    [btn setTitle:textArray[section] forState:0];
    [btn setTitleColor:[UIColor redColor] forState:0];
    btn.backgroundColor = [UIColor grayColor];
    //btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    //btn.titleEdgeInsets = UIEdgeInsetsMake(0, 30, 0, 0);
    [btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    btn.tag = 100 + section;
    return btn;
 
}

- (void)onClick:(UIButton *)btn
{
    NSInteger section = btn.tag - 100;
//    //改变falg
//    if(_sectionFlag[section]==NO){
//        _sectionFlag[section] = YES;
//    }else{
//        _sectionFlag[section] = NO;
//    }
    _sectionFlag[section] = !_sectionFlag[section];
    [self.tableView reloadData];
}

#pragma mark    ------------------------改变段头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    //设置段头高度
    return 40;
}

//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
//{
//    NSArray *textArray = @[@"1",@"2",@"3"];
//    UILabel *label = [[UILabel alloc] init];
//    label.frame = CGRectMake(0, 0, tableView.frame.size.width-50, 40);
//    label.text = textArray[section];
//    label.textColor = [UIColor redColor];
//    //label.textAlignment = NSTextAlignmentCenter;
//    label.font = [UIFont boldSystemFontOfSize:20];
//    label.adjustsFontSizeToFitWidth = YES;
//    label.backgroundColor = [UIColor whiteColor];
//    
//    return label;
//}

#pragma mark    ------------------------设置段尾的高度
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    //设置段尾的高度
    return 1;
}
/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

/*
 #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

技术分享

 

技术分享

 

1.定制section 2.实现类似QQ好友的折叠功能

标签:

原文地址:http://www.cnblogs.com/wanghengheng/p/4844761.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!