天天看點

自定義UITableView顯示不全

我在開發過程中,遇到了自定義UITableView顯示不全的情況,有兩行cell始終拉不到底部。估計是我自定義cell時改變了cell的高導緻的。我的解決辦法是讓UITableView多顯示兩行。下面的數組_allAlarmArray是資料源。在UITableView的協定函數中傳回幾行的函數。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

return  [ _allAlarmArray   count ]+ 2 ;

}

一開始我以為就這樣就可以了。但是運作後的效果沒有任何變化。這個需要注意了,此處還需要在初始化cell的那個協定函數裡面對多出的兩行做處理。就是讓多出的兩行什麼也資料也不顯示。即在協定函數- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中添加幾句代碼如下。

if (indexPath.row >=  [_allAlarmArray count]) {

cell.imageView.image=[UIImage imageNamed:@""];

cell.textLabel.text = @"";

cell.detailTextLabel.text = @"";

return cell;

}。