UITableView

// 設(shè)置cell右邊的指示樣式(箭頭樣式)
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
// 設(shè)置cell右邊顯示的控件 
// accessoryView優(yōu)先級 > accessoryType
cell.accessoryView = [[UISwitch alloc] init];
// 去除多余的分割線
self.tableView.tableFooterView = [[UIView alloc] init];
// 隱藏狀態(tài)欄
- (BOOL)prefersStatusBarHidden {
    return YES;
}
// 快速計(jì)算位置
CGFloat titleX = CGRectGetMaxX(self.iconImageView.frame) + space;

CGFloat priceY = CGRectGetMaxY(self.iconImageView.frame) - space;

// xib 加載自定義cell
if (cell == nil) {
    cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([XMGTgCell class]) owner:nil options:nil] lastObject];
}
    // 代碼注冊cell
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:reuse];
    // xib注冊
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([UITableViewCell class]) bundle:nil] forCellReuseIdentifier:reuse];
  • 利用MJExtension將數(shù)據(jù)轉(zhuǎn)模型
@property (nonatomic, strong) NSArray *statuses;
// 利用MJExtension將數(shù)據(jù)轉(zhuǎn)模型
// 懶加載
- (NSArray *)statuses {
      if (!_statuses) {
          _statuses = [XMGStatus mj_objectArrayWithFilename:@"statuses.plist"];
      }
      return _statuses;
}
  • 計(jì)算文字的尺寸(不需要換行)
計(jì)算Lable內(nèi)文字的尺寸.png
// self--sizing(iOS8 以后)
// 告訴tableView所有cell的真實(shí)高度是自動計(jì)算的(根據(jù)設(shè)置的約束)
self.tableView.rowHeight = UITableViewAutomaticDimension;
// 設(shè)置估算高度
self.tableView.estimatedRowHeight = 44;
屏幕快照 2016-08-03 下午8.28.26.png
  • iOS8之前必須計(jì)算:heightForRowAtIndexPath:代理方法中


    屏幕快照 2016-08-03 下午8.45.24.png
XMGStatusCell *cell; // 定義一個(gè)全局變量(因?yàn)檫@是一個(gè)打醬油的cell)
 // 創(chuàng)建一個(gè)臨時(shí)的cell
 if (cell == nil) {
      cell = [tableView dequeueReusableCellWithTdentifier:ID];
 }
// 修改模型:數(shù)據(jù)源
XMGWine *wine = self.wineArray[0];
wine.money = @"¥100";

// 告訴tableView數(shù)據(jù)變了,趕緊刷新
// 全局刷新
[self.tableView reloadData];

// 局部刷新
NSArray *indexPath = @[[NSIndexPath indexPathForRow:0 inSection:0]];

// 使用前提:保證數(shù)組的個(gè)數(shù)不變
[self.tableView reloadRowsAtIndexPaths:indexPath withRowAnimation:UITableViewRowAnimationLeft];

// 插入
[self.tableView insertRowsAtIndexPaths:indexPath withRowAnimation:UITableViewRowAnimationRight];

// 刪除
[self.tableView deleteRowsAtIndexPaths:indexPath withRowAnimation:UITableViewRowAnimationMiddle];
// 退出編輯模式(右側(cè)編輯按鈕,平滑退出)
    self.tableView.editing = NO;

// 進(jìn)入編輯模式
    // self.tableView.editing = !self.tableView.isEditing; // 無動畫效果
    [self.tableView setEditing:!self.tableView.isEditing animated:YES]; // 帶有動畫效果
    
    self.deletedButton.hidden = !self.tableView.isEditing;
// 告訴tableView在編輯模式下可以多選
    self.tableView.allowsSelectionDuringEditing = YES;
  • 千萬不要一邊遍歷一邊刪除,因?yàn)槊縿h除一個(gè)元素,其他元素的索引可能會發(fā)生變化
   NSMutableArray *deletedWine = [NSMutableArray array];
   for (NSIndexPath *indexPath in self.tableView.indexPathsForSelectedRows) {
       [deletedWine addObject:self.wineArray[indexPath.row]];
   }
   // 修改模型
   [self.wineArray removeObjectsInArray:deletedWine];
   
   // 刷新表格
   // [self.tableView reloadData];
   [self.tableView deleteRowsAtIndexPaths:self.tableView.indexPathsForSelectedRows withRowAnimation:UITableViewRowAnimationAutomatic];
// 取消選中狀態(tài)(在代理方法didSelectRowAtIndexPath:中寫)
[tableView deselectRowAtIndexPath:indexPath animated:YES];
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容