2017-04-21 5 views
0

私はswift 3を使用しており、TableViewを持っています。ユーザーが最後の行にいるときに特定のコードを起動しようとしていますか?なんらかの理由で、それは完全に無視され、発射されません。私はそこにブレークポイントを置いていても、それは当たらない。これは私が持っているものですIOS TableViewが最後の行コードに応答しない

func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { 

     if indexPath.row == self.Locations.count { 
      print("Last Row") 
     } 
} 

printステートメントにブレークポイントも表示されていません。私のテーブルビューのコードは、あなたがself.Locations.count - 1に対する等価性をチェックする必要があり、この

class HomePageC: UIViewController,UITableViewDataSource { 

@IBOutlet weak var TableSource: UITableView! 
var Locations = [String]() 

override func viewDidLoad() { 
    super.viewDidLoad() 

    TableSource.allowsSelection = false 
    TableSource.dataSource = self 
    TableSource.estimatedRowHeight = 504 
    TableSource.showsVerticalScrollIndicator = false 
    TableSource.rowHeight = UITableViewAutomaticDimension 

    TableSource.tableFooterView = UIView() 

} 

func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int { 
    // This always returns 10 items 
    return Locations.count 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> 
    UITableViewCell { 
    // This just populates the fields as I have a custom TableView 
} 

答えて

2

willDisplay()が呼び出されているかどうか確認しましたか?私はそうではないと思う。 func tableView(UITableView, willDisplay: UITableViewCell, forRowAt: IndexPath)UITableViewDelegateメソッドです。あなたのテーブルビューは、デリゲートプロパティを設定するまでそれを呼び出すことはできませんが、これがどこで行われているのかわかりません。

デリゲートは、あなたがdataSourceプロパティにやっているのと同様に、viewDidLoad()に設定することができます。

TableSource.delegate = self 

はまたHomePageCUITableViewDelegateプロトコルを採用していることを宣言する必要があります。これは、クラス宣言で行うことができます:

class HomePageC: UIViewController, UITableViewDataSource, UITableViewDelegate { 
+0

ありがとうございました。 –

1

です。インデックスは0から始まるのでどことしてカウントは、配列内の項目数を返すためLocations.countが

if indexPath.row == (self.Locations.count - 1) { 
    print("Last Row") 
} 

そのを使用してみてください10.

 if indexPath.row == self.Locations.count - 1 { 
      print("Last Row") 
     } 
1

ありながら、最後の項目のindexPath.rowは9になりますインデックスパスは0から始まる反復を返します

関連する問題