?# 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;// 其它的直接點進頭文件看...
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進入視野范圍內就會調用 */- (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;}
// 定義重用標識NSString *ID = @"cell";
// 在這個方法中注冊cell- (void)viewDidLoad { [super viewDidLoad]; // 注冊某個標識對應的cell類型 [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];}
- (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;}
在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
storyboard自定義cell
2.在storyboard中
3.在控制器中
4.在SLDealCell中
xib自定義cell
2.創建一個xib文件(文件名建議跟cell的類名一樣),比如SLDealCell.xib
3.在控制器中
4.在SLDealCell中
代碼自定義cell(使用frame)
1.創建一個繼承自UITableViewCell的子類,比如SLDealCell
在initWithStyle:reuseIdentifier:方法中
2.在控制器中
代碼自定義cell(使用autolayout)
1.創建一個繼承自UITableViewCell的子類,比如SLDealCell
在initWithStyle:reuseIdentifier:方法中
Masonry
)2.在控制器中
// 取消選中的樣式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
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:]
xib自定義cell(重點)
代碼自定義cell(Autolayout)
解決:在設置模型的代碼中,再設置numberOfLines這個屬性,在init方法和awakeFromNib中設置都沒用。因為那個時候還沒有設置UILable的內容,UILable不知道自己需要多高多寬。
解決:設置lable每一行文字的最大寬度
// 只執行一次此方法- (void)awakeFromNib{ // 設置label每一行文字的最大寬度 // 為了保證計算出來的數值 跟 真正顯示出來的效果 一致 self.contentLable.preferredMaxLayoutWidth = [UIScreen mainScreen].bounds.size.width - 20;}
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameCHange:) name:UIKeyboardWillChangeFrameNotification object:nil];
處理方式1
- (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
- (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]; }];}
新聞熱點
疑難解答