麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

UITableView快速入門

2019-11-14 18:47:41
字體:
來源:轉載
供稿:網友

?# UITableViewDatasource

設置數據源

  • 設置數據源的對象必須遵守UITableViewDatasource協議
self.tableView.dataSource = self;

必須實現的數據源方法

@required// 設置每一組有多少行- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;// 設置每一個cell顯示什么,長什么樣子// 什么時候調用:每當有一個cell進入視野范圍內就會調用- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;

其它數據源方法

@optional// 設置有多少組,如果實現此方法,默認返回組數為1- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;// 設置第section組的頭部顯示的string- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;// 設置第section組的尾部顯示的string- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section;// Editing// Individual rows can opt out of having the -editing PRoperty set for them. If not implemented, all rows are assumed to be editable.// 設置每一組是否有可編輯的能力。如果不實現,所有的row假定是可編輯的- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath;// 其它的直接點進頭文件看...

UITableViewDelegate

設置代理

  • 設置的代理對象必須遵守UITableViewDelegate協議
self.tableView.delegate = self;

常見代理方法

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;// custom view for header. will be adjusted to default or specified header height- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;// custom view for footer. will be adjusted to default or specified footer height- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section;// Called after the user changes the selection.- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath;...

Cell的循環利用方式1

/** *  什么時候調用:每當有一個cell進入視野范圍內就會調用 */- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    // 0.重用標識    // 被static修飾的局部變量:只會初始化一次,在整個程序運行過程中,只有一份內存    static NSString *ID = @"cell";    // 1.先根據cell的標識去緩存池中查找可循環利用的cell    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];    // 2.如果cell為nil(緩存池找不到對應的cell)    if (cell == nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];    }    // 3.覆蓋數據    cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row];    return cell;}

Cell的循環利用方式2

  • 定義一個全局變量
// 定義重用標識NSString *ID = @"cell";
  • 注冊某個標識對應的cell類型
// 在這個方法中注冊cell- (void)viewDidLoad {    [super viewDidLoad];    // 注冊某個標識對應的cell類型    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];}
  • 在數據源方法中返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    // 1.去緩存池中查找cell    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];    // 2.覆蓋數據    cell.textLabel.text = [NSString stringWithFormat:@"testdata - %zd", indexPath.row];    return cell;}

Cell的循環利用方式3

  • 在storyboard中設置UITableView的Dynamic Prototypes Cell

  • 設置cell的重用標識

  • 在代碼中利用重用標識獲取cell

// 0.重用標識// 被static修飾的局部變量:只會初始化一次,在整個程序運行過程中,只有一份內存static NSString *ID = @"cell";// 1.先根據cell的標識去緩存池中查找可循環利用的cellUITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];// 2.覆蓋數據cell.textLabel.text = [NSString stringWithFormat:@"cell - %zd", indexPath.row];return cell;

自定義cell

  • 等高的cell

    • storyboard自定義cell

      • 1.創建一個繼承自UITableViewCell的子類,比如SLDealCell


      • 2.在storyboard中

        • 往cell里面增加需要用到的子控件


        • 設置cell的重用標識


        • 設置cell的class為SLDealCell


      • 3.在控制器中

        • 利用重用標識找到cell
        • 給cell傳遞模型數據


      • 4.在SLDealCell中

        • 將storyboard中的子控件連線到類擴展中


        • 需要提供一個模型屬性,重寫模型的set方法,在這個方法中設置模型數據到子控件上


    • xib自定義cell

      • 1.創建一個繼承自UITableViewCell的子類,比如SLDealCell

      • 2.創建一個xib文件(文件名建議跟cell的類名一樣),比如SLDealCell.xib

        • 拖拽一個UITableViewCell出來
        • 修改cell的class為SLDealCell
        • 設置cell的重用標識
        • 往cell中添加需要用到的子控件
      • 3.在控制器中

        • 利用registerNib...方法注冊xib文件
        • 利用重用標識找到cell(如果沒有注冊xib文件,就需要手動去加載xib文件)
        • 給cell傳遞模型數據

      • 4.在SLDealCell中

        • 將xib中的子控件連線到類擴展中
        • 需要提供一個模型屬性,重寫模型的set方法,在這個方法中設置模型數據到子控件上
        • 也可以將創建獲得cell的代碼封裝起來(比如cellWithTableView:方法)
    • 代碼自定義cell(使用frame)

      • 1.創建一個繼承自UITableViewCell的子類,比如SLDealCell

        • 在initWithStyle:reuseIdentifier:方法中

          • 添加子控件
          • 設置子控件的初始化屬性(比如文字顏色、字體
        • 在layoutSubviews方法中設置子控件的frame
        • 需要提供一個模型屬性,重寫模型的set方法,在這個方法中設置模型數據到子控件
      • 2.在控制器中

        • 利用registerClass...方法注冊SLDealCell類
        • 利用重用標識找到cell(如果沒有注冊類,就需要手動創建cell)
        • 給cell傳遞模型數據
        • 也可以將創建獲得cell的代碼封裝起來(比如cellWithTableView:方法)
    • 代碼自定義cell(使用autolayout)

      • 1.創建一個繼承自UITableViewCell的子類,比如SLDealCell

        • 在initWithStyle:reuseIdentifier:方法中

          • 添加子控件
          • 添加子控件的約束(建議使用Masonry
          • 設置子控件的初始化屬性(比如文字顏色、字體)
        • 需要提供一個模型屬性,重寫模型的set方法,在這個方法中設置模型數據到子控件
      • 2.在控制器中

        • 利用registerClass...方法注冊SLDealCell類
        • 利用重用標識找到cell(如果沒有注冊類,就需要手動創建cell)
        • 給cell傳遞模型數據
        • 也可以將創建獲得cell的代碼封裝起來(比如cellWithTableView:方法)

UITableViewCell的常見設置

// 取消選中的樣式cell.selectionStyle = UITableViewCellSelectionStyleNone;// 設置選中的背景色UIView *selectedBackgroundView = [[UIView alloc] init];selectedBackgroundView.backgroundColor = [UIColor redColor];cell.selectedBackgroundView = selectedBackgroundView;// 設置默認的背景色cell.backgroundColor = [UIColor blueColor];// 設置默認的背景色UIView *backgroundView = [[UIView alloc] init];backgroundView.backgroundColor = [UIColor greenColor];cell.backgroundView = backgroundView;// backgroundView的優先級 > backgroundColor// 設置指示器//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;cell.accessoryView = [[UISwitch alloc] init];

強制刷新布局

[self layoutIfNeed];

方法調用順序

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    NSLog(@"cellForRowAtIndexPath");    ...    return cell;}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{    NSLog(@"%s", __func__);    return [self.statues[indexPath.row] cellHeigth];}
  • 打印結果就是調用順序
2015-06-05 19:23:41.159 08-微博-autolayout-xib[1209:325549]-[SLStatusTableViewController tableView:heightForRowAtIndexPath:]2015-06-05 19:23:41.159 08-微博-autolayout-xib[1209:325549]cellForRowAtIndexPath
  • 但是如果實現了tableView:estimatedHeightForRowAtIndexPath:方法的話,調用順序就會改變
2015-06-05 19:25:22.092 08-微博-autolayout-xib[1237:334971]-[SLStatusTableViewController tableView:estimatedHeightForRowAtIndexPath:]2015-06-05 19:25:22.092 08-微博-autolayout-xib[1237:334971]cellForRowAtIndexPath2015-06-05 19:25:22.111 08-微博-autolayout-xib[1237:334971]-[SLStatusTableViewController tableView:heightForRowAtIndexPath:]

自定義非等高CellHeight

  • xib自定義cell(重點)

    • 在模型中增加一個cellHeight屬性,用來存放對應cell的高度
    • 在cell的模型屬性set方法中調用[self layoutIfNeed]方法強制布局,然后計算出模型的cellheight屬性值
    • 在控制器中實現tableView:estimatedHeightForRowAtIndexPath:方法,返回一個估計高度,比如200
    • 在控制器中實現tableView:heightForRowAtIndexPath:方法,返回cell的真實高度(模型中的cellHeight屬性)
  • storyboard自定義cell
  • 代碼自定義cell(frame)
  • 代碼自定義cell(Autolayout)

UILable的寬度問題

  • 問題:代碼創建UILable的時候,設置numberOfLines = 0后,沒有效果。
  • 解決:在設置模型的代碼中,再設置numberOfLines這個屬性,在init方法和awakeFromNib中設置都沒用。因為那個時候還沒有設置UILable的內容,UILable不知道自己需要多高多寬。

  • 如果設置了lable的寬度,文字過多的時候,會報一個錯誤。
  • 解決:設置lable每一行文字的最大寬度

// 只執行一次此方法- (void)awakeFromNib{    // 設置label每一行文字的最大寬度    // 為了保證計算出來的數值 跟 真正顯示出來的效果 一致    self.contentLable.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;}

keyboard

鍵盤彈出來時對視圖的處理

  • 監聽鍵盤的狀態改變
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameCHange:) name:UIKeyboardWillChangeFrameNotification object:nil];
  • 處理方式1

    • transform
- (void)keyboardFrameCHange:(NSNotification *)notification{    CGFloat WH =   [UIScreen mainScreen].bounds.size.height;    CGRect rect =  [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];    CGFloat time = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];// Y方向位移鍵盤的值== 鍵盤的高度[UIView animateWithDuration:time animations:^{    self.view.transform = CGAffineTransformMakeTranslation(0, rect.origin.y - WH);    }];}
  • 處理方式2

    • 約束
    • 取出最下方的底部控件的 對于 self.view的最底部約束(Vertical Space - View - View)
- (void)keyboardFrameCHange:(NSNotification *)notification{    CGFloat WH =   [UIScreen mainScreen].bounds.size.height;    CGRect rect =  [notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];    CGFloat time = [notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue];    // 如果等于鍵盤的y值等于當前的屏幕高度,則表示鍵盤縮回去了    // 反之,則說明鍵盤出來了    // 可以把下面的代碼加入動畫中    if(rect.origin.y != WH)    {        self.bottomSpace.constant = rect.size.height;    }else self.bottomSpace.constant = 0;    [UIView animateWithDuration:time animations:^{        [self.view layoutIfNeeded];    }];}

上一篇:ios濾鏡

下一篇:九宮格的算法

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 免费在线观看国产精品 | 宅男噜噜噜66一区二区 | 一区二区三区日韩在线观看 | 啊~用cao嗯力cao烂我视频 | 亚洲射吧 | 精品国产91久久久 | 久草亚洲视频 | 欧美老外a级毛片 | 日韩黄色免费电影 | 免费观看国产精品视频 | 午夜男人免费视频 | 孕妇体内谢精满日本电影 | 91午夜在线观看 | 国产精品一区免费在线观看 | 91久久久国产精品 | 草操影院| 亚洲一区二区三区日本久久九 | 免费黄色入口 | 国产免费久久久久 | 国产一区不卡 | a视频在线看 | 久久精品久久精品国产大片 | 日韩av电影在线观看 | av在线免费网址 | 欧美日韩亚洲在线 | www.99热精品 | 久草在线最新免费 | 亚洲国产网址 | 日本不卡一二三区 | 国产精品视频一区二区三区四 | 国产一级淫片在线观看 | www久久综合 | 国产精品成年片在线观看, 激情小说另类 | 多男操一女视频 | 在线天堂资源 | 久久精品一区视频 | 中文有码一区二区 | 看免费5xxaaa毛片 | 亚洲国产视频网 | 热久久成人| 91久久久久久久久久久久久 |