2013-12-16 22 views
12

UITableViewに複数の行を持つセクションがあります。セクションの最後の行または最後のセルを取得して、その上に公開インジケータを追加したいとします。私が使用することを考えていUITableviewセクションの最後のセルを取得

一つの方法は次のとおりです。

NSIndexPath *lastCellIndexPath = [NSIndexPath indexPathForItem:[self.tableView numberOfRowsInSection:2]-1 inSection:2]; 

セクションに最後のセルのセルまたはindexpathを得るために、他の最善の方法はありますか?

+0

あなたはデリゲートメソッドで設定し、そこから自分のデータソースからそれを得ることができます。 –

答えて

39
NSInteger totalRow = [tableView numberOfRowsInSection:indexPath.section];//first get total rows in that section by current indexPath. 
    if(indexPath.row == totalRow -1){ 
     //this is the last row in section. 
    } 

希望します。

私はのtableViewでこれをやった:willDisplayCell:forRowAtIndexPath:方法

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath; 
1

numberOfRowsInSectionメソッドで代入メソッド で設定したデータソースから代入メソッドで設定したデータソースから取得できます。

[arrayStores lastObject];

のでcellForRowAtIndexPath方法では、あなたが簡単にそれを確認することができます

3

あなたのcellForRowAtIndexPathメソッドの内部でこれを行う必要があります。このセルがどのセクションに属しているか、それが最後のセクションであるかどうかを簡単に検出できます。

6
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{ 

     //Section 0 
     if (indexPath.section == 0) { 

      // Is Last row? 
      if ([dataSouceArray count] == (indexPath.row+1)) { 
       //Yes 

       cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 


      } 
      else{ 
       // other rows 

       cell.accessoryType = UITableViewCellAccessoryNone; 

      } 


     } 


    } 
+0

この行には誤植をしていませんか? '[NSIndexPath indexPathForRow:[NSIndexPath indexPathForRow:' ?? – sha

+0

そして、このコードはどこに行くのですか? – sha

+0

今すぐコードを確認してください –

0

私はそれを逃すやったのだろうか。最も簡単な解決策はここにあります.. 私はdidSelectRowAtIndexPathメソッドに自分の必要条件に従って書きました。

上記のコードでは、userAccountsListは、tableviewに渡す配列です。

1

スウィフトバージョン:

var totalRow = tableView.numberOfRows(inSection: indexPath.section) 
//first get total rows in that section by current indexPath. 
if indexPath.row == totalRow - 1 { 
    //this is the last row in section. 
} 
関連する問題