iOS中基本控件和使用

1. UIView屬性

Controller的viewDidLoad方法:當(dāng)所有控件加載完畢后調(diào)用赔癌,相當(dāng)于Android中View的onFinishInflate方法;

UIView superview 獲得自己的父控件對(duì)象袖外;

NSArray subview 獲得自己的子控件對(duì)象攒岛,用數(shù)組NSArray保存谎碍;

NSInteger tag 控件的ID標(biāo)示兽叮,父控件可以通過tag來找到對(duì)應(yīng)的子控件;

CGRect fram 控件所在矩形框的位置和尺寸(以父控件的左上角為坐標(biāo)原點(diǎn))卒稳;

CGRect bounds 控件所在矩形框的位置和尺寸(以自己左上角為坐標(biāo)原點(diǎn)蹋半,所以bounds的x\y永遠(yuǎn)為0);

CGPoint center 控件中點(diǎn)的位置(以父控件 的左上角為坐標(biāo)原點(diǎn))充坑;

CGAffineTransform transform 控件的形變屬性(可以設(shè)置旋轉(zhuǎn)角度减江、比例縮放染突、平移等屬性);

UIColor backgroundColor 背景顏色

BOOL hidden 設(shè)置是否隱藏

CGFloat alpha 透明度(0~1)

CGFloat opaque 不透明度(0~1)

BOOL userInteractionEnabled 默認(rèn)YES辈灼,是否可以和用戶交互(相當(dāng)于android enable)

UIViewContentMode contentMode 內(nèi)容顯示的模式(android:gravity =”center_vertical”)

另外UIView中有很多擴(kuò)展協(xié)議份企,用來處理控件的屬性

修改UIView的尺寸(寬高):frame、bounds

修改UIView的位置:frame(左上角的位置)茵休、center(中點(diǎn)的位置)

2. UIView方法

addSubview 添加子控件薪棒,被添加到最上面(subviews中的最后面)

removeFromSuperview 將自己從父控件中移除

viewWithTag 父控件可以根據(jù)這個(gè)tag標(biāo)示找到對(duì)應(yīng)的子控件(遍歷所有的子控件)findViewById

insertSubview:atIndext 添加子控件到指定的位置

beginAnimations: context: 執(zhí)行動(dòng)畫

/…需要執(zhí)行動(dòng)畫的代碼…/

commitAnimations

利用代碼塊block執(zhí)行動(dòng)畫

> /*
> 
> *duration 動(dòng)畫持續(xù)時(shí)間
> 
> *animations 存放需要執(zhí)行動(dòng)畫的代碼
> 
> *completion 存放動(dòng)畫執(zhí)行完畢后需要執(zhí)行的代碼
> 
> */+ (void)animateWithDuration:(NSTimeInterval) duration animations:(void(^)(void))animations completion:(void(^)(BOOLfinished))completion

3. UIControl

只要繼承了UIControl就能簡(jiǎn)單處理一些事件(點(diǎn)擊事件手蝎、值改變事件)榕莺;

繼承了UIControl的子類有:UIButton、UISlider棵介、UISwitch钉鸯、UIDatePicker等等;

當(dāng)需要監(jiān)聽一個(gè)子控件的事件的時(shí)候邮辽,先要看它是否繼承自UIControl唠雕,再看它內(nèi)部是否有delegate屬性(代理);

常用屬性:

enable 是否處理事

contentVerticalAlignment 內(nèi)容在垂直方向上的排布方式

contentHorizontalAlignment 內(nèi)容在水平方向上的排布方式

常用方法

addTarget 添加事件監(jiān)聽器(參數(shù):監(jiān)聽器對(duì)象吨述、事件觸發(fā)回調(diào)方法岩睁、事件類型)

removeTarget 刪除事件監(jiān)聽器

allTargets 獲取所有的監(jiān)聽器對(duì)象集合

常用控件

1. UIImageView 圖片顯示控件(android ImageView)

> @property(weak,nonatomic)IBOutletUIImageView*imageView;self.imageView.image= [UIImageimageNamed:@“icon.png"];

設(shè)置圖片展示模式(android ImageView 的scaleType屬性): 

[圖片上傳失敗...(image-a7eb8-1542002718992)]

2. UISlider可拖動(dòng)的進(jìn)度條 (android SeekBar

> @property(weak,nonatomic)IBOutletUISlider *slider;//設(shè)置最大值self.slider.maximumValue=self.imageDate.count;//設(shè)置最小值self.slider.minimumValue=1;//設(shè)置當(dāng)前值self.slider.value=1;

3. UIButton按鈕

常用屬性

titleLable 獲取內(nèi)部的UILabel對(duì)象

imageView 獲取內(nèi)部的UIImageView對(duì)象

常見方法

setTitle:forState: 設(shè)置背部UILable顯示的文本內(nèi)容(不能寫btn.titleLabel.text = @"123")

setTileColor:forState 設(shè)置內(nèi)部UILabel文字顏色

setTileShaowColor:forState 設(shè)置內(nèi)部UILabel文字的陰影顏色

setImage:forState 設(shè)置內(nèi)部UIImageView的圖片(不能寫btn.iamgeView.image =[ UIImage imagedName:@"0.png"])

setTileShaowColor:forState 設(shè)置內(nèi)部UILabel文字的陰影顏色

setBackgroundImage:forState 設(shè)置背景圖片

下面方法可以獲取不同狀態(tài)下的一些屬性值:

titleForState 獲取某個(gè)狀態(tài)下顯示文字

titleColorForState 獲取某個(gè)狀態(tài)下文字顏色

titleShadowColorForState 獲取某個(gè)狀態(tài)下文字陰影顏色

imageForState 獲取某個(gè)狀態(tài)下圖片

backgroundImageForState 獲取某個(gè)狀態(tài)下背景圖片

下面兩個(gè)方法需要交給子類重寫(繼承UIButton):

titleRectForContentRect 返回內(nèi)部UILabel的frame(位置和尺寸)

imageRectForContentRect 返回內(nèi)部UIImageView的frame(位置和尺寸)

示例代碼

普通按鈕custom

> //創(chuàng)建按鈕
> 
> UIButton*orangeBtn = [[UIButtonalloc]init];
> 
> orangeBtn.tag=kOrangeTag;
> 
> //設(shè)置按鈕的frame(位置和尺寸)
> 
> orangeBtn.frame=CGRectMake(100,100,100,100);
> 
> //設(shè)置背景色
> 
> orangeBtn.backgroundColor= [UIColororangeColor];
> 
> //設(shè)置按鈕文字
> 
> [orangeBtnsetTitle:@"普通"forState:UIControlStateNormal];[orangeBtnsetTitle:@"按下"forState:UIControlStateHighlighted];
> 
> //設(shè)置按鈕按下時(shí)文字顏色
> 
> [orangeBtnsetTitleColor:[UIColorredColor]forState:UIControlStateHighlighted];UIButton*btn = [[UIButtonalloc]init];
> 
> //添加按鈕的唯一標(biāo)示
> 
> btn.tag=1;
> 
> //根據(jù)圖片名稱去項(xiàng)文件系統(tǒng)中加載圖片對(duì)象
> 
> UIImage*imageNormal = [UIImageimageNamed:@"sub_black_prev.png"]];UIImage*imagePress = [UIImageimageNamed:@"sub_blue_prev.png"]];
> 
> //設(shè)置按鈕背景圖片
> 
> [btnsetBackgroundImage:imageNormalforState:UIControlStateNormal];[btnsetBackgroundImage:imagePressforState:UIControlStateHighlighted];
> 
> //設(shè)置按鈕的frame(位置和尺寸)
> 
> //btn.frame = CGRectMake(point.x, point.y, imageNormal.size.width, imageNormal.size.height);/*
> 
>     struct CGRect {
> 
>     CGPoint origin;
> 
>     CGSize size;
> 
>     };
> 
>     */btn.frame= (CGRect){point, imageNormal.size};
> 
> //綁定監(jiān)聽
> 
> [btn addTarget:selfaction:@selector(directionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
> 
> //根據(jù)按鈕的tag獲取子控件
> 
> UIButton*orangeBtn = [self.viewviewWithTag:kOrangeTag];CGPointcenter = orangeBtn.center;staticCGFloatDELETE =50;center.y+= DELETE;
> 
> //重新賦值按鈕的中點(diǎn)orangeBtn.center= center;

根據(jù)系統(tǒng)自帶樣式類型創(chuàng)建

//創(chuàng)建一個(gè)加號(hào)的按鈕UIButton*btn = [UIButtonbuttonWithType:UIButtonTypeContactAdd];btn.center= CGPointMake(100,100);

4. UILable 文本 (android TextView)

常用屬性:

text 顯示文本內(nèi)容

textColor 文字顏色

font 字體

shadowColor 文字的陰影顏色

shadowOffset 陰影的偏差距離(width水平方向的偏差距離,整數(shù)右邊揣云;height垂直方向上的偏差距離捕儒,正數(shù)下邊)

textAlignment 設(shè)置文字的排列方式(偏左、偏右邓夕、居中)

numberOfLines 允許文字最多幾行(默認(rèn)1刘莹,如果是0,自動(dòng)換行)

5. UIText 文本輸入框(android EditText)

6. UISwitch 開關(guān)

#pragma mark 開關(guān)值改變監(jiān)聽-(IBAction)switchChanged:(UISwitch *)sender {NSLog(@"開關(guān)值%d", sender.isOn);self.view.backgroundColor= sender.isOn?[UIColordarkGrayColor]:[UIColorwhiteColor];}

7. UIDatePicker日期控件

> -(void) viewDidLoad{   
> 
> [superviewDidLoad];   
> 
> UIDatePicker *picker = [[UIDatePicker alloc] init];
> 
> //設(shè)置區(qū)域picker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
> 
> //設(shè)置模式(顯示日期還是時(shí)間)picker.datePickerMode = UIDatePickerModeDate;}#pragma mark 拖動(dòng)時(shí)間改變后監(jiān)聽
> 
> -(IBAction)dateChanged:(UIDatePicker *) dataPicker{
> 
> //獲取日期值
> 
> NSDate *date= dataPicker.date;
> 
> //格式化日期
> 
> NSDateFormatter *formatter = [[NSDateFormatter alloc] init];   
> 
> formatter.dateFormat = @"yyyy/MM/bb";   
> 
> self.label.text = [formatter stringFromDate:date];}

8. UIPickerView 跟android Spinner相似

設(shè)置數(shù)據(jù)源和代理(監(jiān)聽)為控制器焚刚,控制器遵循數(shù)據(jù)源河代理的協(xié)議点弯,重寫協(xié)議的方法:

簡(jiǎn)單使用

> #pragma mark 控制器充當(dāng)UIPickerView的數(shù)據(jù)源和代理,必須遵循兩個(gè)協(xié)議@interface ViewController : UIViewController@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
> 
> @end
> 
> #import "ViewController.h"
> 
> @interface ViewController()
> 
> @property (nonatomic,strong) NSArray *oneCol;
> 
> @property (nonatomic,strong) NSArray *towCol;
> 
> @end
> 
> @implementation ViewController
> 
> -(void) viewDidLoad{
> 
>     self.oneCol = @[@"00", @"01", @"02", @"03"];
> 
>     self.towCol = @[@"000", @"001", @"002", @"003", @"004"];
> 
> }
> 
> #pragma mark - UIPickerView的數(shù)據(jù)源方法(返回第component列的行數(shù))
> 
> -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
> 
>     return component == 0?self.oneCol.count:self.towCol.count;
> 
> }
> 
> #pragma mark - UIPickerView的數(shù)據(jù)源方法(返回列數(shù))
> 
> -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
> 
>     return 2;
> 
> }
> 
> #pragma mark - UIPickerView的代理方法(返回第component列第row航顯示的字符串?dāng)?shù)據(jù)-設(shè)置數(shù)據(jù))
> 
> -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
> 
>     if(component==0){
> 
>         return self.oneCol[row];
> 
>     }else{
> 
>         return self.towCol[row];
> 
>     }
> 
> }
> 
> #pragma mark - UIPickerView的代理方法(選中了某一行調(diào)用)
> 
> -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
> 
>     if(component==0){
> 
>         NSString *leftRow = self.oneCol[row];
> 
>         //獲取第二列當(dāng)前停留的行數(shù)
> 
>         int towRow = [pickerView selectedRowInComponent:1];
> 
>         NSString *rightRow = self.towCol[towRow];
> 
>         NSLog(@"第%d列:%@-----第%d列:%@", component, leftRow, 1, rightRow);
> 
>     }else{
> 
>         NSString *rightRow = self.towCol[row];
> 
>         int oneRow = [pickerView selectedRowInComponent:0];
> 
>         NSString *leftRow = self.oneCol[oneRow];
> 
>         NSLog(@"第%d列:%@-----第%d列:%@", 0, leftRow, 1, rightRow);
> 
>     }
> 
> }
> 
> @end

顯示自定義條目

> #pragma mark 修改每一行的高度-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
> 
>     return 70;
> 
> }#pragma mark 返回地component列第row行需要顯示的控件-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
> 
>     UIView *rowView = [[UIView alloc] init];
> 
>     CGFloat rowViewH = 40;
> 
>     CGFloat rowViewW = 200;
> 
>     rowView.bounds = CGRectMake(0, 0, 200, rowViewH);
> 
>     //國(guó)家名稱    UILabel *nameLable = [[UILabel alloc] init];
> 
>     CGFloat nameW = 70;
> 
>     nameLable.frame = CGRectMake(0, 0, nameW, rowViewH);
> 
>     nameLable.textAlignment = NSTextAlignmentCenter;
> 
>     nameLable.text = @"asdfsd";
> 
>     [rowView addSubview:nameLable];
> 
>     //國(guó)旗    UIImageView *imageView = [[UIImageView alloc] init];
> 
>     imageView.frame = CGRectMake(nameW, 0, rowViewW-nameW, rowViewH);
> 
>     imageView.image = [UIImage imageNamed:@"zhongguo.jpg"];
> 
>     [rowView addSubview:imageView];
> 
>     return rowView;
> 
> }

9. UIScollView (android ScollView)

UIPageControl 請(qǐng)見:分頁顯示

當(dāng)子控件內(nèi)容太多或者子控件太大顯示不全時(shí)矿咕,用UIScrollView實(shí)現(xiàn)滾動(dòng)

常用屬性

CGPoint contentOffset UIScrollView當(dāng)前滾動(dòng)到哪個(gè)位置了(相對(duì)于內(nèi)容左上角的坐標(biāo))

CGSize contentSize UIScrollView的滾動(dòng)范圍(內(nèi)容的尺寸)

UIEdgeInsets contentInset 這個(gè)屬性可以在四周增加滾動(dòng)范圍

[圖片上傳失敗...(image-52733c-1542002718992)]

BOOL bounces; 是否有彈簧效果

BOOL scrollEnabled; 是否能滾動(dòng)

BOOL showsHorizontalScrollIndicator 是否顯示水平方向的滾動(dòng)條

BOOL showsVerticalScrollIndicator 是否顯示垂直方向的滾動(dòng)條

UIScrollViewIndicatorStyle indicatorStyle 設(shè)定滾動(dòng)條的樣式

BOOL tracking 是否正在被拖拽

BOOL dragging 當(dāng)touch后還沒有拖動(dòng)的時(shí)候值是YES抢肛,否則是NO

BOOL decelerating 是否正在減速

使用示例:

> UIImage *image = [UIImage imageNamed:@"1.png"];
> 
> //通過圖片初始化UIImageview,這樣ImageView的寬高就跟圖片寬高一樣了
> 
> UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
> 
> [self.scroll addSubview:imageView];
> 
> //self.scroll.frame.size  UIScrollView控件的大刑贾(可視范圍)
> 
> //設(shè)置內(nèi)容的寬高(可滾動(dòng)范圍)
> 
> self.scroll.contentSize = image.size;
> 
> //為UIScrollView設(shè)置上下左右額外的可拖動(dòng)范圍(空隙)
> 
> self.scroll.contentInset = UIEdgeInsetsMake(10, 20, 30, 40);
> 
> self.scroll.backgroundColor = [UIColor lightGrayColor];
> 
>   //self.scroll.contentOffset.x += 10;//不能直接修改對(duì)象的結(jié)構(gòu)體屬性的成員
> 
> //修改UIScrollView當(dāng)前拖動(dòng)的位置(相對(duì)于內(nèi)容左上角的坐標(biāo))
> 
> CGPoint offset = self.scroll.contentOffset;
> 
> //向左移動(dòng)(查看右邊的內(nèi)容)
> 
> offset.x += 50;
> 
> //執(zhí)行動(dòng)畫
> 
> [UIView animateWithDuration:0.3 animations:^{
> 
>     self.scroll.contentOffset = offset;
> 
> }];

手勢(shì)識(shí)別縮放

@property(nullable,nonatomic,weak)id delegate;

1

iOS中的UIScrollView已經(jīng)封裝了手勢(shì)識(shí)別雌团,不需要我們分析手指位置以及滑動(dòng)方向,只需要設(shè)置上面的代理屬性即可(代理可以理解為回調(diào)接口)士聪,這個(gè)代理需要遵守UIScrollViewDelegate協(xié)議锦援;然后選擇性的實(shí)現(xiàn)里面的回調(diào)方法。

而android中如果要實(shí)現(xiàn)此功能剥悟,需要我們分析多點(diǎn)觸控以及手指位置和滑動(dòng)方向來判斷縮放還是放大灵寺。

步驟:

①曼库、設(shè)置UIScrollView的代理delegate為控制器,控制器遵守UIScrollViewDelegate協(xié)議

②略板、設(shè)置最大和最小縮放比例maximumZoomScale毁枯,minimumZoomScale

③、讓代理對(duì)象實(shí)現(xiàn)方法叮称,返回需要縮放的子控件

> //設(shè)置最大縮放比例
> 
> self.scroll.maximumZoomScale = 2;
> 
> //設(shè)置最小縮放比例
> 
> self.scroll.minimumZoomScale = 0.5;
> 
> #pragma mark -UIScrollView的代理方法:返回需要進(jìn)行縮放的空間(必須是UIScrollVIew的子控件)
> 
> (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
> 
>     return self.imageView;
> 
> }

代理中其他方法:

> - (void)scrollViewDidScroll:(UIScrollView *)scrollView; // any offset changes offset屬性變化時(shí)調(diào)用(在被拖動(dòng))
> 
> // 將要開始拖拽時(shí)調(diào)用
> 
> - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView;     
> 
> // 拖拽將要結(jié)束時(shí)調(diào)用
> 
> - (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset NS_AVAILABLE_IOS(5_0);
> 
> // 拖拽結(jié)束時(shí)調(diào)用
> 
> - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate;
> 
> - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView;  // called on finger up as we are moving
> 
> - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView;      // ScrollView減速完畢后調(diào)用                                           
> 
> - (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView; // called when setContentOffset/scrollRectVisible:animated: finishes. not called if not animating
> 
> - (nullable UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView;    // 返回需要進(jìn)行縮放的子控件
> 
> - (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view NS_AVAILABLE_IOS(3_2); // 將要縮放之前調(diào)用
> 
> - (void)scrollViewDidZoom:(UIScrollView *)scrollView NS_AVAILABLE_IOS(3_2); // any zoom scale changes 縮放時(shí)調(diào)用
> 
> - (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(nullable UIView *)view atScale:(CGFloat)scale; // 縮放完畢后調(diào)用
> 
> - (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView;  // return a yes if you want to scroll to the top. if not defined, assumes YES
> 
> - (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView;      // called when scrolling animation finished. may be called immediately if already at top

分頁顯示(android ViewPager)

[圖片上傳失敗...(image-8798eb-1542002718991)]

> #define kCount 5
> 
> @interface ViewController()
> 
> @property (nonatomic, weak)UIPageControl *_pageControl;
> 
> @property (nonatomic, weak) UIScrollView*_scrollView;
> 
> @end
> 
> @implementation ViewController
> 
> - (void)viewDidLoad {
> 
>    [superviewDidLoad];
> 
>    for(int i = 0; I
> 
>         [self.view.subviews[i] removeFromSuperview];
> 
>     }
> 
>     //分頁顯示
> 
>     //1种玛、添加UIScrollView
> 
>     UIScrollView *scrollView = [[UIScrollViewalloc] init];
> 
>     //設(shè)置填充父窗體
> 
>     scrollView.frame = self.view.bounds;
> 
>     [self.viewaddSubview:scrollView];
> 
>     self._scrollView= scrollView;
> 
>     CGFloat scrollWdith = scrollView.frame.size.width;
> 
>     CGFloat scrollHight = scrollView.frame.size.height;
> 
>     // 2、添加所有的ImageView
> 
>     for(int i = 0;i<=kCount;i++){
> 
>         //加載圖片
> 
>         NSString*imageName = [NSString stringWithFormat:@"pages.bundle/%d.jpg", I];
> 
>         UIImage *image = [UIImageimageNamed:imageName];
> 
>         UIImageView *imageView = [[UIImageViewalloc] init];
> 
>         imageView.image= image;
> 
>         imageView.frame = CGRectMake((i-1)*scrollWdith, 0, scrollWdith, scrollHight);
> 
>         [scrollViewaddSubview:imageView];
> 
>     }
> 
>     //3瓤檐、設(shè)置滾動(dòng)范圍
> 
>     scrollView.contentSize = CGSizeMake(kCount*scrollWdith, 0);
> 
>     //隱藏滾動(dòng)條
> 
>     scrollView.showsHorizontalScrollIndicator = NO;
> 
>     //4赂韵、開啟分頁功能(按照ScrollView的寬度將內(nèi)容分為若干頁,正好ImageView的寬度相等挠蛉,就實(shí)現(xiàn)了分頁)
> 
>     scrollView.pagingEnabled = YES;
> 
>     //5祭示、添加PageControl
> 
>     UIPageControl *pageControl =[[UIPageControl alloc] init];
> 
>     pageControl.bounds = CGRectMake(0, 0, 150, 50);
> 
>     pageControl.center = CGPointMake(scrollWdith * 0.5,
> 
> scrollHight - 50);
> 
>     //設(shè)置頁數(shù)
> 
>     pageControl.numberOfPages= kCount;
> 
>     //當(dāng)前選中頁碼對(duì)應(yīng)控制器的顏色
> 
>     pageControl.currentPageIndicatorTintColor = [UIColorredColor];
> 
>     //其他控制器的顏色
> 
>     pageControl.pageIndicatorTintColor = [UIColorblackColor];
> 
>     //監(jiān)聽pageControl事件
> 
>     [pageControl addTarget:self action:@selector(pageChanged:)forControlEvents:UIControlEventValueChanged];
> 
>     //添加控制器
> 
>     [self.viewaddSubview: pageControl];
> 
>     self._pageControl= pageControl;
> 
>     //設(shè)置代理
> 
>     scrollView.delegate = self;
> 
>     //不需要彈簧效果
> 
>     scrollView.bounces = NO;
> 
> }
> 
> #pragma mark -滾動(dòng)代理:減速完畢后調(diào)用(停止了)
> 
> - (void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView{
> 
>     CGFloat offset = scrollView.contentOffset.x;   //當(dāng)前x軸方向上滾動(dòng)了多少
> 
>     int pageNum = offset/scrollView.frame.size.width;
> 
>     //設(shè)置頁碼
> 
>     self._pageControl.currentPage= pageNum;
> 
> }
> 
> #pragma mark
> 
> pageControl值變化時(shí)調(diào)用
> 
> -(void) pageChanged{
> 
>     CGFloat offsetX = self._pageControl.currentPage * self.view.frame.size.width;
> 
>     [UIView beginAnimations:nilcontext:nil];
> 
>     self._scrollView.contentOffset = CGPointMake(offsetX, 0);
> 
>     [UIViewcommitAnimations];
> 
> }
> 
> @end
> 
> ew?'qкX

10. UITableView( android ListView GridView ExpandListView)

10.1 數(shù)據(jù)源方法

TableView相當(dāng)于ListView,設(shè)置數(shù)據(jù)源需要遵循UITableViewDataSource協(xié)議谴古,相當(dāng)于BaseAdapter质涛,下面是協(xié)議的所有接口:

> @protocol UITableViewDataSource
> 
> @required   必須實(shí)現(xiàn)的
> 
> - (NSInteger)tableView:(UITableView *)tableView
> 
> numberOfRowsInSection:(NSInteger)section;   // 第section組有多少行數(shù)據(jù)
> 
> // 返回每一行顯示的具體數(shù)據(jù)View(相當(dāng)于getView()方法)
> 
> - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath;
> 
> @optional  選擇實(shí)現(xiàn)的
> 
> - (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView;  // 一共有多少組數(shù)據(jù)(如果沒有實(shí)現(xiàn),默認(rèn)為1)
> 
> - (nullable NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; // 分組頭部標(biāo)題
> 
> - (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section; // 分組尾部標(biāo)題
> 
> // Individual
> 
> rows can opt out of having the -editing property set for them. If not
> 
> implemented, all rows are assumed to be editable.
> 
> - (BOOL)tableView:(UITableView*)tableView canEditRowAtIndexPath:(NSIndexPath*)indexPath;
> 
> //
> 
> Moving/reordering
> 
> // Allows the
> 
> reorder accessory view to optionally be shown for a particular row. By default,
> 
> the reorder control will be shown only if the datasource implements
> 
> -tableView:moveRowAtIndexPath:toIndexPath:
> 
> - (BOOL)tableView:(UITableView*)tableView canMoveRowAtIndexPath:(NSIndexPath*)indexPath;
> 
> //索引(e.g. "ABCD...Z#")
> 
> - (nullable NSArray *)sectionIndexTitlesForTableView:(UITableView*)tableView __TVOS_PROHIBITED;                                                 
> 
> - (NSInteger)tableView:(UITableView*)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index__TVOS_PROHIBITED;  // tell table
> 
> which section corresponds to section title/index (e.g. "B",1))
> 
> // Data
> 
> manipulation - insert and delete support
> 
> // After a row
> 
> has the minus or plus button invoked (based on the UITableViewCellEditingStyle
> 
> for the cell), the dataSource must commit the change
> 
> // Not called
> 
> for edit actions using UITableViewRowAction - the action's handler will be
> 
> invoked instead
> 
> - (void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
> 
> forRowAtIndexPath:(NSIndexPath*)indexPath;
> 
> // Data
> 
> manipulation - reorder / moving support
> 
> - (void)tableView:(UITableView *)tableView
> 
> moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
> 
> toIndexPath:(NSIndexPath*)destinationIndexPath;
> 
> @end
> 

10.2 代理 UITableViewDelegate

> // 返回條目高度(可以根據(jù)不同的行號(hào)返回不同的高度)
> 
> - (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath;
> 
> - (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section;
> 
> - (CGFloat)tableView:(UITableView*)tableView heightForFooterInSection:(NSInteger)section;
> 
> //  選中某一行的時(shí)候調(diào)用(點(diǎn)擊)
> 
> -(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath
> 
> // 取消選中某一行
> 
> -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
> 

10.3 常用屬性和方法

[圖片上傳失敗...(image-f6906d-1542002718991)]

10.4 多組數(shù)據(jù)

[圖片上傳失敗...(image-cac2f2-1542002718991)]

> @interface ViewController()
> 
>      @property (nonatomic, strong) NSArray*gd;
> 
>      @property (nonatomic, strong) NSArray*hn;
> 
> @end
> 
> @implementation ViewController
> 
> - (void)viewDidLoad {
> 
>     [superviewDidLoad];
> 
>     self.gd = @[@"廣州",
> 
> @"東莞",
> 
> @"惠州"];
> 
>     self.hn = @[@"常德",
> 
> @"長(zhǎng)沙",
> 
> @"湘潭",
> 
> @"株洲",
> 
> @"岳陽",
> 
> @"湘西自治州"];
> 
> }
> 
> #pragma mark - 數(shù)據(jù)源方法
> 
> #pragma mark - 1掰担、一共多少組數(shù)據(jù)
> 
> -(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{
> 
>     return 2;   //廣東汇陆、湖南
> 
> }
> 
> #pragma mark - 2、第section組有多少行數(shù)據(jù)
> 
> -(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
> 
>    return section==0?self.gd.count:self.hn.count;
> 
> }
> 
> #pragma mark - 3带饱、返回每一行顯示的具體數(shù)據(jù)
> 
> -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
> 
>     // 組 indexPath.section
> 
>     // 列 indexPath.row
> 
>     UITableViewCell *cell = [[UITableViewCell alloc]
> 
> initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
> 
>     NSString *city = nil;
> 
>     if(indexPath.section == 0){
> 
>         city =self.gd[indexPath.row];
> 
>     }else{
> 
>         city =self.hn[indexPath.row];
> 
>     }
> 
>     //設(shè)置右邊箭頭樣式
> 
>     cell.accessoryType= UITableViewCellAccessoryDisclosureIndicator;
> 
>     //設(shè)置cell上面顯示的文本數(shù)據(jù)
> 
>     cell.textLabel.text= city;
> 
>     returncell;
> 
> }
> 
> #pragma mark - 4毡代、 第section組的header標(biāo)題
> 
> -(NSString *)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{
> 
>     return section == 0? @"廣州" :
> 
> @"湖南";
> 
> }
> 
> #pragma mark - 5、 第section組的尾部標(biāo)題
> 
> -(NSString *)tableView:(UITableView*)tableView titleForFooterInSection:(NSInteger)section{
> 
>     return section == 0? @"廣東很多帥哥":
> 
> @"湖南很多美女";
> 
> }
> 
> #pragma mark- 6纠炮、 索引月趟,通訊錄效果,是按照分組而不是拼音首字母的索引
> 
> - (nullable NSArray *)sectionIndexTitlesForTableView:(UITableView*)tableView __TVOS_PROHIBITED{
> 
>     return @[@"廣州",
> 
> @"湖南",
> 
> @"湖南",
> 
> @"湖南",
> 
> @"湖南",
> 
> @"湖南",
> 
> @"湖南",
> 
> @"湖南",
> 
> @"湖南",
> 
> @"湖南",
> 
> @"湖南",
> 
> @"湖南",
> 
> @"湖南",
> 
> @"湖南",
> 
> @"湖南",
> 
> @"湖南",
> 
> @"湖南",
> 
> @"湖南",
> 
> @"湖南",
> 
> @"湖南",
> 
> @"湖南",
> 
> @"湖南",
> 
> @"湖南",
> 
> @"湖南",
> 
> @"湖南",
> 
> @"湖南"];
> 
> }
> 
> #pragma mark-
> 
> UITableView代理方法
> 
> #pragma mark- 1恢口、 返回某一行的高度
> 
> - (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{
> 
>     return 70;
> 
> }
> 
> #pragma mark- 2孝宗、 選中某一行的時(shí)候調(diào)用(點(diǎn)擊)
> 
> -(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
> 
>     NSString *city = indexPath.section== 0? self.gd[indexPath.row] : self.hn[indexPath.row];
> 
>     //彈出對(duì)話框修改地名
> 
>     UIAlertView *alert =[[UIAlertView alloc] initWithTitle:@"地名" message:nil delegate:selfcancelButtonTitle:@"取消" otherButtonTitles:@"確定", nil];
> 
>     //設(shè)置alertview樣式
> 
>     alert.alertViewStyle= UIAlertViewStylePlainTextInput;
> 
>     //取出文本輸入框
> 
>     [alert textFieldAtIndex:0].text= city;
> 
>     alert.tag = 1;
> 
>     //顯示對(duì)話框
> 
>     [alert show];
> 
> }
> 
> #pragma mark-
> 
> UIAlertView代理方法
> 
> #pragma mark- 對(duì)話框按鈕點(diǎn)擊
> 
> - (void)alertView:(UIAlertView *)alertView
> 
> clickedButtonAtIndex:(NSInteger)buttonIndex
> 
> NS_DEPRECATED_IOS(2_0, 9_0){
> 
>     if(buttonIndex == 1){
> 
>         //點(diǎn)擊了確定按鈕
> 
>         //取得輸入框文本
> 
>         NSString *city = [alertView textFieldAtIndex:0].text;
> 
>         NSString *old = self.gd[0];
> 
>         old = [NSString stringWithFormat:@"%@",city];
> 
>         //刷新UITableView數(shù)據(jù),相當(dāng)于android 的 adaptert.notifyDatesetChanged()
> 
>         //刷新所有數(shù)據(jù)
> 
> //        [self.tableView reloadData];
> 
>         //局部刷新
> 
>         NSIndexPath *path = [NSIndexPath indexPathForRow:alertView.tag inSection:0];
> 
>         [self.tableViewreloadRowsAtIndexPaths:@[path]withRowAnimation:UITableViewRowAnimationLeft];
> 
>     }
> 
> }
> 
> @end
> 

10.5 單組數(shù)據(jù)(ListView效果)

數(shù)據(jù)源返回分組數(shù)量的方法返回值為1就能實(shí)現(xiàn)單組效果

UITableViewCell系統(tǒng)樣式:

[圖片上傳失敗...(image-5a38a6-1542002718991)]

條目右邊箭頭樣式:

[圖片上傳失敗...(image-1a5660-1542002718991)]

10.6 性能優(yōu)化

復(fù)用Cell

> #pragma mark 每當(dāng)有一個(gè)cell進(jìn)入視野范圍內(nèi)就會(huì)調(diào)用耕肩,返回當(dāng)前這行顯示的cell
> 
> - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
> 
> {
> 
>     // 0.用static修飾的局部變量因妇,只會(huì)初始化一次< # # >
> 
>     static NSString *ID = @"Cell";
> 
>     // 1.拿到一個(gè)標(biāo)識(shí)先去緩存池中查找對(duì)應(yīng)的Cell
> 
>     UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:ID];
> 
>     // 2.如果緩存池中沒有,才需要傳入一個(gè)標(biāo)識(shí)創(chuàng)建新的Cell
> 
>     if (cell == nil) {
> 
>         cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
> 
>     }
> 
>     // 3.覆蓋數(shù)據(jù)
> 
>     cell.textLabel.text = [NSString stringWithFormat:@"
> 
> fdsfdsf-%d", indexPath.row];
> 
>     returncell;
> 
> }
> 

用戶代碼提醒

Xcode中有很多系統(tǒng)自帶的代碼塊猿诸,只需要拖動(dòng)到編輯器中婚被,就能自動(dòng)生成代碼,這樣可以避免寫很多重復(fù)代碼梳虽,方便編程址芯;也可以自定義用戶代碼塊,比如上面復(fù)用Cell的模板方法,選中代碼后拖動(dòng)到右下角代碼塊中即可谷炸。

[圖片上傳失敗...(image-84db53-1542002718991)]

10.7 編輯模式

[圖片上傳失敗...(image-3d52b8-1542002718991)]

刪除\添加

開啟編輯模式:

> // 開啟編輯模式
> 
>     //self.tableView.editing
> 
> = YES;
> 
>     //[self.tableView
> 
> setEditing:YES];
> 
>     // 帶有動(dòng)畫效果
> 
>     [self.tableView setEditing:! edt animated:YES];

下面方法的返回值決定編輯模式是添加還是刪除:

> #pragma mark - 代理方法
> 
> #pragma mark 當(dāng)tableview開啟編輯模式就會(huì)調(diào)用
> 
> -(UITableViewCellEditingStyle)tableView:(UITableView *)tableVieweditingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
> 
> {
> 
>     //return
> 
> indexPath.row%2 ? UITableViewCellEditingStyleDelete :
> 
> UITableViewCellEditingStyleInsert;
> 
>     returntableView.tag;
> 
> }
> 

實(shí)現(xiàn)數(shù)據(jù)源的方法刪除或添加數(shù)據(jù):

> #pragma mark 提交編輯操作(點(diǎn)擊了"刪除"或者"+"按鈕)時(shí)調(diào)用
> 
> // 實(shí)現(xiàn)了這個(gè)方法北专,就有左劃刪除功能
> 
> - (void)tableView:(UITableView*)tableViewcommitEditingStyle:(UITableViewCellEditingStyle)editingStyle  forRowAtIndexPath:(NSIndexPath*)indexPath
> 
> {
> 
>     //NSLog(@"------commit---%d",
> 
> indexPath.row);
> 
>     if(editingStyle == UITableViewCellEditingStyleDelete) {
> 
>         // 刪除
> 
>         // 1.更改數(shù)據(jù)(刪除本行的數(shù)據(jù))
> 
>         [self.mydata removeObjectAtIndex:indexPath.row];
> 
>         // 2.刷新UI界面
> 
>         //[tableView
> 
> reloadData];
> 
>         [tableViewdeleteRowsAtIndexPaths:@[indexPath]withRowAnimation:UITableViewRowAnimationLeft];
> 
>     }else{
> 
>         // 添加
> 
>         // 1.更改數(shù)據(jù)(添加數(shù)據(jù))
> 
>         // [self.mydata
> 
> addObject:@" hahahhahah"];添加到最后面去了
> 
>         // 插入數(shù)據(jù)到本行的后面
> 
>         [self.mydata insertObject:@"新添加的數(shù)據(jù)...." atIndex:indexPath.row +1];
> 
>         // 2.刷新UI界面
> 
>         //[tableView
> 
> reloadData];
> 
>         // 刷新指定行(個(gè)數(shù)不變)
> 
>         //[tableView
> 
> reloadRowsAtIndexPaths:<#(NSArray *)#>
> 
> withRowAnimation:<#(UITableViewRowAnimation)#>];
> 
>         // 刪除指定行
> 
>         //[tableView
> 
> deleteRowsAtIndexPaths:<#(NSArray *)#>
> 
> withRowAnimation:<#(UITableViewRowAnimation)#>];
> 
>         // 插入新的行
> 
>         NSIndexPath *newPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:0];
> 
>         [tableViewinsertRowsAtIndexPaths:@[newPath] withRowAnimation:UITableViewRowAnimationTop];
> 
>     }
> 
> }

排序

如果實(shí)現(xiàn)了數(shù)據(jù)源UITableViewDataSource的moveRowAtIndexPath方法,就會(huì)有排序拖動(dòng)功能旬陡,但這只是界面表面的變化拓颓,數(shù)據(jù)順序并沒有變,所以在這個(gè)方法中描孟,需要改變數(shù)據(jù)順序驶睦。

> #pragma mark 如果實(shí)現(xiàn)了這個(gè)方法, 就會(huì)有排序功能
> 
> - (void)tableView:(UITableView*)tableView moveRowAtIndexPath:(NSIndexPath*)sourceIndexPath toIndexPath:(NSIndexPath*)destinationIndexPath
> 
> {
> 
>     // 取出即將要?jiǎng)h除的數(shù)據(jù)
> 
>    NSString *data = self.mydata[sourceIndexPath.row];
> 
>     // 刪除需要移動(dòng)的那一行
> 
>     [self.mydataremoveObject:data];
> 
>     // 插入之前刪除的數(shù)據(jù)
> 
>     [self.mydata insertObject:data atIndex:destinationIndexPath.row];
> 
> }
> 

4個(gè)刷新UI界面的方法

添加新的行:

[tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationTop];

刪除指定的行:

[tableView deleteRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationTop]

局部刷新指定的行:

[tableView reloadRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationTop]

整體刷新所有的行:

[tableView reloadData]

10.8 自定義Cell

步驟:

①匿醒、新建xid來描述Cell(拖一個(gè)UITableViewCell設(shè)置寬高以及identify)场航;

②、實(shí)現(xiàn)數(shù)據(jù)源和代理方法青抛,返回?cái)?shù)據(jù)和Cell的高度(heightForRowAtIndexPath )旗闽;

③酬核、拿到Cell中的子控件綁定數(shù)據(jù)(根據(jù)tag)蜜另;

④、拿到按鈕綁定監(jiān)聽器(根據(jù)事件取得事件點(diǎn)對(duì)應(yīng)的行號(hào))

> #pragma mark 每當(dāng)有一個(gè)cell進(jìn)入視野范圍內(nèi)就會(huì)調(diào)用嫡意,返回當(dāng)前這行顯示的cell
> 
> - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
> 
> {
> 
>     // 0.用static修飾的局部變量举瑰,只會(huì)初始化一次
> 
>     static NSString *ID = @"Cell";
> 
>     // 1.拿到一個(gè)標(biāo)識(shí)先去緩存池中查找對(duì)應(yīng)的Cell
> 
>     UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:ID];
> 
>     // 2.如果緩存池中沒有,才需要傳入一個(gè)標(biāo)識(shí)創(chuàng)建新的Cell
> 
>     if (cell == nil) {
> 
>         // 通過 xib文件來加載cell
> 
>         NSBundle *bundle = [NSBundlemainBundle];
> 
>         NSArray * objs = [bundle loadNibNamed:@"BookCell" owner:nil options:nil];
> 
>         cell = [ objs lastObject];
> 
>         // 綁定監(jiān)聽器
> 
>         UIButton*collect  = (UIButton *)[cell viewWithTag:3];
> 
>         [collect addTarget:self action:@selector(collectBook:event:)forControlEvents:UIControlEventTouchUpInside];
> 
>     }
> 
>     //cell.tag =
> 
> indexPath.row + 1000;
> 
>     // 3.覆蓋數(shù)據(jù)
> 
>     // 3.1 取出book對(duì)象
> 
>     Book *book =self.books[indexPath.row];
> 
>     // 3.2 設(shè)置名稱
> 
>     UILabel *nameLabel = (UILabel *)[cell viewWithTag:1];
> 
>     nameLabel.text = book.name;
> 
>     returncell;
> 
> }

cell中的按鈕添加點(diǎn)擊事件

通過點(diǎn)擊事件點(diǎn)的位置蔬螟,確定cell的索引:

> // 得到觸摸事件對(duì)象---> 得到觸摸點(diǎn) --->得到觸摸點(diǎn)在UITableView中的位置 ---> 得到觸摸點(diǎn)在UITableView中的行號(hào)
> 
> - (void)collectBook:(UIButton*) btn event:(UIEvent *)event
> 
> {
> 
>     //NSLog(@"----%@",
> 
> event);
> 
>     UITableView *tableView = (UITableView *)self.view;
> 
>     // 獲取所有的觸摸點(diǎn)(UITouch對(duì)象此迅,如果是單點(diǎn)觸碰,就只有1個(gè)UITouch)
> 
>     NSSet *touches = [eventallTouches];
> 
>     // 一個(gè)UITouch對(duì)象對(duì)應(yīng)一根手指
> 
>     UITouch *touch = [touchesanyObject];
> 
>     // 獲取觸摸點(diǎn)在UITableView上面的的位置
> 
>     CGPointposition = [touch locationInView:tableView];
> 
>     // 根據(jù)觸摸位置 得到 對(duì)應(yīng)的行號(hào)
> 
>     NSIndexPath*indexPath = [tableView indexPathForRowAtPoint:position];
> 
>     //NSLog(@"%d",
> 
> indexPath.row);
> 
>     Book *book =self.books[indexPath.row];
> 
>     NSLog(@"%@", book.name);
> 
> }
> 

Cell屬性封裝:

根據(jù)tag找控件(android根據(jù)id找控件)旧巾,tag太多不好管理耸序,效率不高,如果控件太多代碼量大:

定義一個(gè)類繼承UITableViewCell鲁猩,xid指向這個(gè)類坎怪,子控件作為此類的屬性即可,創(chuàng)建Cell時(shí)用這個(gè)類接受:

[圖片上傳失敗...(image-1625e9-1542002718991)]

10.9 九宮格

iOS中沒有Android中的GridView廓握,九宮格是由UITableView實(shí)現(xiàn)的搅窿,自定義Cell,每一行Cell中放入numClunk(多少列) 個(gè)按鈕隙券。

10.10 qq聯(lián)系人列表

iOS中沒有Android中的ExpandListView男应,如果需要實(shí)現(xiàn)分組展開合并功能,只需要控制返回那個(gè)分組返回的行數(shù)娱仔,行數(shù)為0就是合并了沐飘。

自定義分組標(biāo)題

> #pragma mark 第section組對(duì)應(yīng)的標(biāo)題
> 
> - (UIView *)tableView:(UITableView *)tableView
> 
> viewForHeaderInSection:(NSInteger)section
> 
> {
> 
>     HeaderView *titleView =[HeaderView buttonWithType:UIButtonTypeCustom];
> 
>     // 設(shè)置標(biāo)題內(nèi)容
> 
>     NSDictionary *dict = self.allFriends[section];
> 
>     [titleView setTitle:dict[@"group"] forState:UIControlStateNormal];
> 
>     // 設(shè)置標(biāo)題顏色
> 
>     [titleView setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];
> 
>     // 設(shè)置背景顏色
> 
>     [titleView setBackgroundColor:[UIColorgrayColor]];
> 
>     // 設(shè)置按鈕的tag為組號(hào)
> 
>     titleView.tag= section;
> 
>     // 監(jiān)聽標(biāo)題點(diǎn)擊
> 
>     [titleView addTarget:self action:@selector(titleClick:)forControlEvents:UIControlEventTouchUpInside];
> 
>     // 取出第section組的狀態(tài)
> 
>     int result = [self.status[@(section)]intValue];
> 
>     // 對(duì)狀態(tài)進(jìn)行取反
> 
>     if (result == 0) {
> 
>         titleView.imageView.transform= CGAffineTransformIdentity;
> 
>     }else{
> 
>         titleView.imageView.transform= CGAffineTransformMakeRotation(M_PI_2);
> 
>     }
> 
>     returntitleView;
> 
> }
> 

合并展開分組

> #pragma mark - 監(jiān)聽標(biāo)題點(diǎn)擊
> 
> - (void)titleClick:(UIButton*)btn {
> 
>     // 取出標(biāo)題按鈕對(duì)應(yīng)的組號(hào)
> 
>     int section = btn.tag;
> 
>     // 取出第section組的狀態(tài)
> 
>     int result = [self.status[@(section)]intValue];
> 
>     // 對(duì)狀態(tài)進(jìn)行取反
> 
>     if (result == 0) {
> 
>         [self.status setObject:@1forKey:@(section)];
> 
>     }else{
> 
>         [self.status setObject:@0forKey:@(section)];
> 
>     }
> 
>     // 刷新數(shù)據(jù)(會(huì)重新給數(shù)據(jù)源發(fā)送消息)
> 
>     [self.tableViewreloadData];
> 
> }
> 
> //- (NSString
> 
> *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
> 
> //{
> 
> //    // 1.獲取這組對(duì)應(yīng)的字典數(shù)據(jù)
> 
> //    NSDictionary *dict =self.allFriends[section];
> 
> //   
> 
> //    return dict[@"group"];
> 
> //}
> 
> #pragma mark 每一組中有多少行數(shù)據(jù)
> 
> - (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
> 
> {
> 
>     // 取出第section組對(duì)應(yīng)的狀態(tài)
> 
>     int result = [self.status[@(section)]intValue];
> 
>     if (result == 0) { // 合并狀態(tài)
> 
>         return 0;
> 
>     }else { // 展開狀態(tài)
> 
>         // 1.先獲取這組對(duì)應(yīng)的數(shù)據(jù)
> 
>         NSDictionary *group = self.allFriends[section];
> 
>         // 2.獲取這組里面的好友
> 
>         NSArray *friends = group[@"friends"];
> 
>         return friends.count;
> 
>     }
> 
> }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子耐朴,更是在濱河造成了極大的恐慌众弓,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,496評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件隔箍,死亡現(xiàn)場(chǎng)離奇詭異谓娃,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)蜒滩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,407評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門滨达,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人俯艰,你說我怎么就攤上這事捡遍。” “怎么了竹握?”我有些...
    開封第一講書人閱讀 162,632評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵画株,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我啦辐,道長(zhǎng)谓传,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,180評(píng)論 1 292
  • 正文 為了忘掉前任芹关,我火速辦了婚禮续挟,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘侥衬。我一直安慰自己诗祸,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,198評(píng)論 6 388
  • 文/花漫 我一把揭開白布轴总。 她就那樣靜靜地躺著直颅,像睡著了一般。 火紅的嫁衣襯著肌膚如雪怀樟。 梳的紋絲不亂的頭發(fā)上功偿,一...
    開封第一講書人閱讀 51,165評(píng)論 1 299
  • 那天,我揣著相機(jī)與錄音漂佩,去河邊找鬼。 笑死投蝉,一個(gè)胖子當(dāng)著我的面吹牛养葵,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播瘩缆,決...
    沈念sama閱讀 40,052評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼关拒,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起着绊,我...
    開封第一講書人閱讀 38,910評(píng)論 0 274
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎归露,沒想到半個(gè)月后洲脂,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,324評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡剧包,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,542評(píng)論 2 332
  • 正文 我和宋清朗相戀三年恐锦,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片疆液。...
    茶點(diǎn)故事閱讀 39,711評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡一铅,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出堕油,到底是詐尸還是另有隱情潘飘,我是刑警寧澤,帶...
    沈念sama閱讀 35,424評(píng)論 5 343
  • 正文 年R本政府宣布掉缺,位于F島的核電站卜录,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏攀圈。R本人自食惡果不足惜暴凑,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,017評(píng)論 3 326
  • 文/蒙蒙 一峦甩、第九天 我趴在偏房一處隱蔽的房頂上張望赘来。 院中可真熱鬧,春花似錦凯傲、人聲如沸犬辰。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,668評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽幌缝。三九已至,卻和暖如春诫欠,著一層夾襖步出監(jiān)牢的瞬間涵卵,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,823評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工荒叼, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留轿偎,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,722評(píng)論 2 368
  • 正文 我出身青樓被廓,卻偏偏與公主長(zhǎng)得像坏晦,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,611評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容