uitableView review yesterday’s knowledge :

 
 
 
 
folding group :
 
--------------------------------------------------------------------------------------------------------------------------------------------------
teacher code : 
 
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    QFGroupHead *headView=[[NSBundle mainBundle]loadNibNamed:@"QFGroupHead" owner:nil options:nil][0];
    [headView addTarget:self action:@selector(onHeadClicked:) forControlEvents:UIControlEventTouchUpInside];
    headView.tag=section;
    QFQQGroupModel *model=dataArray[section];
    [headView refreshUIWithModel:model];
    return headView;
}
 
-(void)onHeadClicked:(UIView *)headView{
    NSLog(@"onHeadClicked:%d",headView.tag);
    QFQQGroupModel *model=dataArray[headView.tag];
    model.isHide=!model.isHide;
    [self.myTableView reloadSections:[NSIndexSet indexSetWithIndex:headView.tag] withRowAnimation:UITableViewRowAnimationNone];
}
 
 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    //在数据源中取出数据模型
    QFQQGroupModel *model=dataArray[section];
    if (model.isHide) {
        return 0;
    }
    //返回数据模型中相应的数据
    return model.groupMember.count;
}
 
teacher ’s dataModel :
 
@property (nonatomic,copy) NSString *groupName;
@property int friendNum;
@property (nonatomic,strong) NSMutableArray *groupMember;
@property BOOL isHide;
 
 
 
--------------------------------------------------------------------------------------------------------------------------------------------------
My code :
 
 
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    GroupHead *groupHead = [[NSBundle mainBundle] loadNibNamed:@"GroupHead" owner:nil options:nil][0];
    
    [groupHead addTarget:self action:@selector(onHeadClicked:) forControlEvents:UIControlEventTouchUpInside];
    groupHead.tag = section;
    [groupHead refreshUIWithModel:self.model modelSection:section];
    return groupHead;
}
 
-(void)onHeadClicked:(UIView *)headView{
    
    NSNumber *number = self.model.isHiddenArray[headView.tag];
    if(number.intValue == 1)
        [self.model isHiddenSection:headView.tag Boolis:NO];
    else
        [self.model isHiddenSection:headView.tag Boolis:YES];
    
    [self.myTableView reloadSections:[NSIndexSet indexSetWithIndex:headView.tag] withRowAnimation:UITableViewRowAnimationNone];
    
}
 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSNumber *number = self.model.isHiddenArray[section];
    if(number.intValue){
        return 0;
    }
    return [self.model.groupMemberNum[section] intValue];
}
 
my DataModel :
 
@property (nonatomic, strong) NSMutableArray *groupName;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableArray *groupMemberNum;
@property (nonatomic, strong) NSMutableArray *isHiddenArray;
@property int groupNum;
-(void)begin;
-(void)isHiddenSection:(NSInteger)section Boolis:(BOOL)isbool;
@end
 
 
DataModel implement :
 
-(void)begin{
    _dataArray = [NSMutableArray array];
    _groupName = [NSMutableArray array];
    _groupMemberNum = [NSMutableArray array];
    _isHiddenArray = [NSMutableArray array];
    _groupNum = 10;
    for (int i=0; i<10; ++i) {
        NSMutableArray *groupArray = [NSMutableArray array];
        NSString *groupstr = [NSString stringWithFormat:@"Group %d", i];
        [_groupName addObject:groupstr];
        int num = arc4random()%10+1;
        NSNumber *nsNum = [[NSNumber alloc] initWithBool:NO];
        [_isHiddenArray addObject:nsNum];
        [_groupMemberNum addObject:[NSString stringWithFormat:@"%d", num]];
        for (int j=0; j<num; ++j) {
            NSString *str = [NSString stringWithFormat:@"cell %d", j];
            [groupArray addObject:str];
        }
        [_dataArray addObject:groupArray];
    }
}

-(void)isHiddenSection:(NSInteger)section Boolis:(BOOL)isbool{
    NSNumber *num = [[NSNumber alloc] initWithBool:isbool];
    [_isHiddenArray removeObjectAtIndex:section];
    [_isHiddenArray insertObject:num atIndex:section];
}

 
 
GroupHead :
 
-(void)refreshUIWithModel:(Datamodel*)model modelSection:(NSInteger)section{
    groupName.text = model.groupName[section];
    totalMember.text = [NSString stringWithFormat:@"TotalMember:%@", model.groupMemberNum[section]];
    NSNumber *number = model.isHiddenArray[section];
    if(number.intValue == 0)
        hiddenStatus.text = @">";
    else hiddenStatus.text = @"v";
}
 
--------------------------------------------------------------------------------------------------------------------------------------------------
 
Search Bar :
 
     完成搜索功能的步骤
     
     1、先实例化一个全局数组,用于储存搜索结果。
     2、实例化一个UISearchBar,并设置为tableview的tableheadview。
     3、用上面实例化的seachbar初始化一个搜索结果显示视图(UISearchDisplayController)
     4、设置uiseachDisplayController的三个代理
     //searchDC内部有一个talbview,这里设置代理就等于给它内部的tableview设置代理
     searchDC.searchResultsDataSource=self;
     searchDC.searchResultsDelegate=self;
     //searchDC自己的代理
     searchDC.delegate=self;
     5、导入UISearchDisplayDelegate协议
     6、实现UISearchDisplayDelegate的一个重要的代理方法
     - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
     每当搜索栏的文字发生变化时,都会调用这个代理方法
     在这个方法里面我们遍历整个数据源,找到包含搜索文字的那些,然后放进搜索结果的数组里面(注意,搜索之前要记得把搜索结果的数组先清空)
     7、由于myTableView和搜索结果的tableview共用我们这个类的那些代理方法,所以要在所有的代理方法里面加入判断,以返回正确的数据。
     例如:
     //有多少组
     -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
     //判断是否为searchDisplayController的结果tableview
     if (tableView==searchDC.searchResultsTableView) {
     return 1;
     }
     return totalArray.count;
     }
     、、、、、
     
     8、至此,搜索功能完成。
     
     */
 
 
 
 
--------------------------------------------------------------------------------------------------------------------------------------------------
 
my Search Bar Code ::
 
//
//  MainViewController.m
//  UITableViewCellXib_01
//
//  Created by YuLei on 4/11/14.
//  Copyright (c) 2014 ___DuanYuLei___. All rights reserved.
//

#import "MainViewController.h"
#import "GroupHead.h"

@interface MainViewController () <UITableViewDataSource, UITableViewDelegate, UISearchDisplayDelegate]]>
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@end

@implementation MainViewController{
    UISearchDisplayController *searchDisplayController;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    _model = [[Datamodel alloc] init];
    [_model begin];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    //search function
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
    self.myTableView.tableHeaderView = searchBar;
    
    searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    searchDisplayController.searchResultsDataSource = self;
    searchDisplayController.searchResultsDelegate = self;
    searchDisplayController.delegate = self;

//create a colorful foot line or clear the line.
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 1)];
    footView.backgroundColor = [UIColor clearColor];
    self.myTableView.tableFooterView = footView;
}

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

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(tableView == searchDisplayController.searchResultsTableView)
        return 0;
    return 44;
}

-(void)onHeadClicked:(UIView *)headView{
    
    NSNumber *number = self.model.isHiddenArray[headView.tag];
    
    if(number.intValue == 1)
        [self.model isHiddenSection:headView.tag Boolis:NO];
    else
        [self.model isHiddenSection:headView.tag Boolis:YES];
    
    [self.myTableView reloadSections:[NSIndexSet indexSetWithIndex:headView.tag] withRowAnimation:UITableViewRowAnimationNone];
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    GroupHead *groupHead = [[NSBundle mainBundle] loadNibNamed:@"GroupHead" owner:nil options:nil][0];
    if (tableView == searchDisplayController.searchResultsTableView) {
        return nil;
    }
    
    [groupHead addTarget:self action:@selector(onHeadClicked:) forControlEvents:UIControlEventTouchUpInside];
    groupHead.tag = section;
    [groupHead refreshUIWithModel:self.model modelSection:section];
    return groupHead;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (tableView == searchDisplayController.searchResultsTableView) {
        return self.model.searchDataArray.count;
    }
    
    NSNumber *number = self.model.isHiddenArray[section];
    if(number.intValue){
        return 0;
    }
    return [self.model.groupMemberNum[section] intValue];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if(tableView == searchDisplayController.searchResultsTableView)
        return 1;
    return self.model.groupNum;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuseID = @"myCell";
    UITableViewCell *cell = [_myTableView dequeueReusableCellWithIdentifier:reuseID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    }
    
    if(tableView == searchDisplayController.searchResultsTableView){
        cell.textLabel.text = self.model.searchDataArray[indexPath.row];
    }else{
        cell.textLabel.text = self.model.dataArray[indexPath.section][indexPath.row];
    }
    return cell;
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
    [self.model.searchDataArray removeAllObjects];
    for (int i=0; i<self.model.groupNum; ++i) {
        NSArray *friends = self.model.dataArray[i];
        for (int j=0; j<friends.count; ++j) {
            NSString *str = friends[j];
            NSRange range = [str rangeOfString:searchString];
            if (range.length > 0) {
                [self.model.searchDataArray addObject:str];
            }
        }
    }
    return YES;
}

@end

 
--------------------------------------------------------------------------------------------------------------------------------------------------
 
Add a groupHeader cache
 
 
define it in :
 
@implementation MainViewController{
    UISearchDisplayController *searchDisplayController;
    NSMutableDictionary *cacheHeadViewDictionary;
}
 
- (void)viewDidLoad{
    [super viewDidLoad];
    cacheHeadViewDictionary=[NSMutableDictionary dictionary];
 
 
use it as function  <tableView:viewForHeaderInSection>
 
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (tableView == searchDisplayController.searchResultsTableView) {
        return nil;
    }
    
    GroupHead *groupHead = cacheHeadViewDictionary[[NSString stringWithFormat:@"%d", section]];
    if (groupHead == nil) {
        groupHead = [[NSBundle mainBundle] loadNibNamed:@"GroupHead" owner:nil options:nil][0];
        groupHead.backgroundColor=[UIColor colorWithRed:(CGFloat)random()/RAND_MAX green:(CGFloat)random()/RAND_MAX blue:(CGFloat)random()/RAND_MAX alpha:1];
        [groupHead addTarget:self action:@selector(onHeadClicked:) forControlEvents:UIControlEventTouchUpInside];
        groupHead.tag = section;
        [cacheHeadViewDictionary setObject:groupHead forKey:[NSString stringWithFormat:@"%d",section]];
    }
    [groupHead refreshUIWithModel:self.model modelSection:section];
    return groupHead;
}
 
 
--------------------------------------------------------------------------------------------------------------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 

IOS UI 第十篇: UITABLEVIEW的更多相关文章

  1. IOS UI 第十一篇: UITABLEVIEW

    DIY a tableviewcell :   - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *) ...

  2. IOS UI 第八篇:基本UI

    实现图片的滚动,并且自动停止在每张图片上     - (void)viewDidLoad{    [super viewDidLoad]; UIScrollView *scrollView = [[U ...

  3. UI第十八节——UITableView

    在iOS开发中UITableView可以说是使用最广泛的控件,我们平时使用的软件中到处都可以看到它的影子,基本大部分应用都有UITableView.当然它的广泛使用自然离不开它强大的功能,今天就针对U ...

  4. iOS UI基础-9.0 UITableView基础

    在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView.UITableView继承自UIScrollView,因此支持垂直滚动,而且性能极佳. UITableView有两种样式: ...

  5. IOS UI 第六篇:基本UI

    加两个UI模块   - (void)viewDidLoad{    [self begin1];    [self begin2];    [super viewDidLoad];    // Do ...

  6. IOS UI 第五篇:基本UI

    添加个导航栏:         Xib1 *xib1 = [[Xib1 alloc] initWithNibName:@"Xib1" bundle:nil];    UINavig ...

  7. iOS UI基础-9.2 UITableView 简单微博列表

    概述 我们要实现的效果: 这个界面布局也是UITableView实现的,其中的内容就是UITableViewCell,只是这个UITableViewCell是用户自定义实现的.虽然系统自带的UITab ...

  8. iOS UI基础-9.1 UITableView 团购

    概述 接下来,我们要做的是团购界面的设计,最张要实现的效果图及项目结构图      团购数据的展示 思路: 系统自带的tableCell不能展示三个文本,不能满足条件,自定义tableCell 每一个 ...

  9. IOS UI 第四篇:基本UI

    ViewController 应用   再第一个XIB页面创建另一个XIB页面,并且通过按钮调用它     - (IBAction)GoSecond:(id)sender {    secondVie ...

随机推荐

  1. DevExpress.XtraReports.UI.XtraReport 动态报表

    原文:DevExpress.XtraReports.UI.XtraReport 动态报表 using System;using System.Collections.Generic;using Sys ...

  2. ubuntu12.04下一个samba、tftp、nfs构造

    1.samba setting   1>apt-get install samba     apt-get install smbfs   2>mkdir /home/linux/shar ...

  3. Dos命令将合并两个文本文件的内容

    当生产线的问题,有一个放b.txt的内容被添加到a.txt这需要采取.在考虑这个问题.我的第一感觉是敲代码.阅读b.txt内容,渐进写a.txt.想起昨天在加工处理生产线600M决的方法,我用java ...

  4. ASP.NET MVC+EF框架+EasyUI实现权限管理系列(2)-数据库访问层的设计Demo

    原文:ASP.NET MVC+EF框架+EasyUI实现权限管理系列(2)-数据库访问层的设计Demo ASP.NET MVC+EF框架+EasyUI实现权限管系列 (开篇) (1)框架搭建 前言:这 ...

  5. js之with

    网上找资料的时候看到有js代码里出现了大量的with语句,有点好奇,这里做下总结: 1)简要说明         with 语句可以方便地用来引用某个特定对象中已有的属性,但是不能用来给对象添加属性. ...

  6. jquery扩展

    jQuery插件的开发包括两种: 一种是类级别的插件开发,即给jQuery添加新的全局函数,相当于给jQuery类本身添加方法.jQuery的全局函数就是属于jQuery命名空间的函数,另一种是对象级 ...

  7. ArcGIS API for Silverlight 使用GP服务实现要素裁剪功能

    原文:ArcGIS API for Silverlight 使用GP服务实现要素裁剪功能 昨天一QQ好友问了一个关于裁剪的问题,感觉自己也没有帮上什么忙,之后自己做了一个裁剪的例子,不过在做这个例子的 ...

  8. C# mongodb 1

    转载C# mongodb 概述 MongoDB是一个高性能,开源,无模式的文档型数据库,使用C++开发.是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.他 ...

  9. POJ 3373 Changing Digits 好蛋疼的DP

    一開始写的高位往低位递推,发现这样有些时候保证不了第四条要求.于是又開始写高位往低位的记忆化搜索,又发现传參什么的蛋疼的要死.然后又发现高位開始的记忆化搜索就是从低位往高位的递推呀,遂过之. dp[i ...

  10. vistual studio 2012 安装失败,提示Microsoft Vistual Studio 2012 Devenv找不到元素,等错误信息

    在安装vistual studio 2012过程中,出现安装失败,提示Microsoft Vistual Studio 2012 Devenv找不到元素,等错误信息 解决方法是更新相应的server补 ...