国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

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

UITableView隨筆筆記

2019-11-14 20:29:54
字體:
來源:轉載
供稿:網友

UITableView 繼承自UIScrollView,所以可以滾動,但只能是縱方向上的。

UITableView由section和cell組成,填充的內容來自數據源,一般數據源由ViewController作為代理,因此需要遵循它的兩個協議,分別是UITableViewDataSource 和 UITableViewDelegate。

UITableView的簡單實現步驟:

1. 設置UITableView在視圖中,可以表現為Plain和Grouped兩種風格;

2. 將UITableView的dataSource設置為ViewController,并讓ViewController遵循UITableViewDataSource協議;

3. 利用DataSource協議設置UITableView的sections有多少個,調用方法numberOfSectionsInTableView,返回sections的個數;

4. 設置UITableView的每個section中有多少行,調用方法numberOfRowsInSection,返回sections的行數;

5. 設置UITableView中sections的首部標題,調用方法titleForHeaderInSection,尾部標題方法為titleForFooterInSection;

6.1 設置cell中顯示的內容,調用方法cellForRowAtIndexPath。通過傳入的(NSIndexPath *)indexPath來確定具體的row來給定內容,最終返回UITableViewCell類型的cell對象;

6.2 其中NSIndexPath包含了section和row,可以準確定位是哪個section中的哪個row;

6.3 取得的數據內容將傳遞給UITableViewCell中的textLable的text屬性

UITableViewCellStyle有四種模式,當設置為UITableViewCellStyleSubtitle的時候可以增加副標題文字說明效果;

依然在cellForRowAtIndexPath中設置UITableView的相關屬性:

6.4 在UITableViewCell中有imageView屬性,可以設置每個cell的圖標,將圖片名傳給image屬性;

6.5 副標題設置是detailTextLabel,是個UILabel,可設置內容顏色等;

6.6 accessoryType可設置accessory類型,是選項或是箭頭指示;

 

如果修改了數據模型,需要讓viewcontroller通知view刷新一下,刷新方法分為全部和局部;

1.reloadData 將刷新全部數據

2.reloadRowsAtIndexPaths 刷新局部數據

- (void)reloadRowsAtIndexPaths:(NSArray *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation

indexPaths是NSIndexPath類型的數組,animation為枚舉類型的UITableViewRowAnimation,設置刷新的動畫效果;

 

UITableView的數據優化

之前6.1中設置cell中顯示的內容,調用方法cellForRowAtIndexPath內,在對UITableViewCell實例化的時候,用到的:

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

其中reuseIdentifier就是設置重用標示符,將不顯示的數據會丟入緩存池中,然后即將顯示的cell會在緩存池中通過標示符來查找,如果找到該標示符的cell就直接拿過來重用,避免重新實例化消耗內存影響性格,達到優化目的;如果沒找到對應標示符的cell就實例化一個。具體代碼如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *reuseIdentifierStr = @"myCell";    //在緩沖池中尋找    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifierStr];    //如果沒找到,就實例化一個新的cell    if (!cell) {        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifierStr];    }    PRoduct *p = data[indexPath.row];    cell.textLabel.text = p.name;    cell.imageView.image = [UIImage imageNamed:p.icon];    cell.detailTextLabel.text = p.detail;    cell.detailTextLabel.textColor = [UIColor grayColor];    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;        return cell;}

 

7. UITableView中cell的添加刪除操作,使用到Toolbar的添加刪除按鈕

7.1 在添加刪除按鈕中設置UITableViewCellEditingStyle的標識,1為刪除,2為添加。因為該枚舉類型是只讀的,所以只能通過對tableview的tag進行設置,來識別是添加操作還是刪除操作。

- (IBAction)action:(UIBarButtonItem *)sender {    if (sender.tag == 1) {        _tableView.tag = UITableViewCellEditingStyleDelete;    }    else{        _tableView.tag = UITableViewCellEditingStyleInsert;    }    BOOL isEdit = _tableView.isEditing;    [_tableView setEditing:!isEdit];}

 

7.2 標識需要在方法editingStyleForRowAtIndexPath中的返回值設置,這樣就知道了是刪除操作還是添加操作。

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{    return _tableView.tag;}

 

7.3 相應的操作需要在commitEditingStyle中實現

- (void) tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{    //刪除操作    if (editingStyle == 1) {        //刪除數據        [_dataList removeObjectAtIndex:indexPath.row];        //更新cell        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];    }else{        //添加數據        [_dataList insertObject:@"新建數據" atIndex:indexPath.row + 1];        //更新cell        NSIndexPath *insertIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];        [tableView insertRowsAtIndexPaths:@[insertIndexPath] withRowAnimation:UITableViewRowAnimationTop];    }}

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 重庆市| 望奎县| 吉林省| 江山市| 泾源县| 青海省| 江口县| 海宁市| 重庆市| 施秉县| 驻马店市| 贺兰县| 同心县| 万源市| 彭水| 尼玛县| 卓尼县| 始兴县| 安吉县| 册亨县| 景宁| 休宁县| 安溪县| 保康县| 武陟县| 鄂托克前旗| 陆良县| 昆明市| 望城县| 永靖县| 乌鲁木齐市| 巢湖市| 亚东县| 铜陵市| 长武县| 垫江县| 小金县| 湖北省| 吉林市| 德格县| 青州市|