IOS UI 第十篇: UITABLEVIEW

uitableView review yesterday’s knowledge :


51b98580e49e1148c1b89427c0fbe0e8 2
 
 
 
 
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.tagwithRowAnimation: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,copyNSString *groupName;
@property int friendNum;
@property (nonatomic,strongNSMutableArray *groupMember;
@property BOOL isHide;
 
 
 
--------------------------------------------------------------------------------------------------------------------------------------------------
My code :
 
 
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
GroupHead *groupHead = [[NSBundle mainBundleloadNibNamed:@"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.tagwithRowAnimation: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 (nonatomicstrongNSMutableArray *groupName;
@property (nonatomicstrongNSMutableArray *dataArray;
@property (nonatomicstrongNSMutableArray *groupMemberNum;
@property (nonatomicstrongNSMutableArray *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 allocinitWithBool: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 allocinitWithBool: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,并设置为tableviewtableheadview
     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 () <UITableViewDataSourceUITableViewDelegateUISearchDisplayDelegate]]>
@property (weaknonatomicIBOutlet 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 allocinit];
    [
_model begin];
    
self.myTableView.delegate = self;
    
self.myTableView.dataSource = self;
    
    
//search function
    
UISearchBar *searchBar = [[UISearchBar allocinitWithFrame:CGRectMake(0032030)];
    
self.myTableView.tableHeaderView = searchBar;
    
    
searchDisplayController = [[UISearchDisplayController allocinitWithSearchBar:searchBar contentsController:self];
    
searchDisplayController.searchResultsDataSource = self;
    
searchDisplayController.searchResultsDelegate = self;
    
searchDisplayController.delegate = self;

    
    
    
//create a colorful foot line or clear the line.
    
UIView *footView = [[UIView allocinitWithFrame:CGRectMake(003201)];
    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.tagwithRowAnimation:UITableViewRowAnimationNone];
    
}


- (
UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
GroupHead *groupHead = [[NSBundle mainBundleloadNibNamed:@"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 allocinitWithStyle: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 mainBundleloadNibNamed:@"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;
}
 
 
--------------------------------------------------------------------------------------------------------------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
原文地址:https://www.cnblogs.com/firstrate/p/3662136.html