2016-11-05 2 views
7

私は2つのファイルを持っています。cell.swiftでindexPath.rowを取得する方法

  • myTableViewController.swift
  • myTableCell.swift

私はmyTabelCell.swift機能でindexPath.rowを得ることができますか?

はここmyTableViewController.swift

class myTableViewController: UITableViewController { 

    //Default func 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     //automatic row height 
     tableView.estimatedRowHeight = 450 
     tableView.rowHeight = UITableViewAutomaticDimension 



    } 

// cell config 
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 


     //define cell 
     let cell = tableView.dequeueReusableCell(withIdentifier: "myTableCell", for: indexPath) as! myTableCell 



} 

あなたが見ることができるように...私はmyTableCell、liktBtnTapped機能でindexPath.rowを取得しようとしている。ここ

import UIKit 
import Parse 
import ActiveLabel 

class myTableCell : UITableViewCell { 

    //Button 
    @IBOutlet weak var commentBtn: UIButton! 
    @IBOutlet weak var likeBtn: UIButton! 
    @IBOutlet weak var moreBtn: UIButton! 


    override func awakeFromNib() { 
     super.awakeFromNib() 


    } 

    @IBAction func likeBtnTapped(_ sender: AnyObject) { 

     //declare title of button 
     let title = sender.title(for: UIControlState()) 

     //I want get indexPath.row in here! 

    } 

myTableCell.swift

です。

私はIndexPath.rowにアクセスしたり取得したりできますか教えてください。

答えて

17

UIView(これはUIResponderから継承されます)で使用できる再帰的メソッドを使用して、特定のタイプの親ビューを見つけるために拡張子UIResponderを作成しました。これを利用

import UIKit 

extension UIResponder { 

    func next<T: UIResponder>(_ type: T.Type) -> T? { 
     return next as? T ?? next?.next(type) 
    } 
} 

、我々は、テーブルビューとセルのインデックスパスのためのいくつかの便利な読み出し専用計算特性を有するUITableViewCellを拡張することができます。それ

import UIKit 
import Parse 
import ActiveLabel 

class myTableCell : UITableViewCell { 

//Button 
@IBOutlet weak var commentBtn: UIButton! 
@IBOutlet weak var likeBtn: UIButton! 
@IBOutlet weak var moreBtn: UIButton! 


override func awakeFromNib() { 
    super.awakeFromNib() 


} 


} 


class myTableViewController: UITableViewController { 

//Default func 
//assuming you have an array for your table data source 
var arrayOfTitles = [String]() 
override func viewDidLoad() { 
    super.viewDidLoad() 

    //automatic row height 
    tableView.estimatedRowHeight = 450 
    tableView.rowHeight = UITableViewAutomaticDimension 



} 

// cell config 
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 


//define cell 
let cell = tableView.dequeueReusableCell(withIdentifier: "myTableCell", for: indexPath) as! myTableCell 
cell.commentBtn.tag = indexPath.row 
cell.commentBtn.addTarget(self, action: #selector(likeBtnTapped(_:), forControlEvents:.TouchUpInside) 


//cell config end 


@IBAction func likeBtnTapped(sender: UIButton) { 
let btn = sender 
let indexP = NSIndexPath(forItem: btn.tag, inSection: 0) 
let cell = tableView.dequeueReusableCell(withIdentifier: "myTableCell", for: indexP) as! myTableCell 

    //I want get indexPath.row in here! 
    let title = arrayOfTitles[indexP.row] 

    //declare title of button 
    cell.commentBtn.setTitle(title, forState: UIControlState.Normal) 



} 


} 
+0

素晴らしい拡張子と思われます。あなたはどのように使用するコードを入れてもらえますか? –

+0

'UITableViewCell + IndexPath.swift'という名前の新しいファイルを作成し、上記のコードを追加するだけです。次に、セルの内部で、 'self.indexPath'を使ってインデックスパスを取得できます。 – Callam

+0

ありがとう〜!それはうまく動作します –

3

簡単..あなたは、この内部のボタンアクションのように行うことができます:cellForRowAtIndexPath

let section = 0 
let row = sender.tag 
let indexPath = IndexPath(row: row, section: section) 
let cell: myTableCell = self.feedTableView.cellForRow(at: indexPath) as! myTableCell 

、その後:

// add the row as the tag 
cell.button.tag = indexPath.row 
+0

コメントは自分のコードに貼り付けてください。 likeBtnTapped関数にコードを貼り付けました。しかし、自己。 .. –

+0

私はあなたのテーブルビュー**セル**コントローラではありません –

+0

ああ今私は理解していますありがとう –

1

は、セルクラスにプロパティindexPathを作成し、cellForRowAtIndexPathでそれを設定しますセルが再利用されます。

警告があります。セルを並べ替えるテーブルビューメソッドの中には、cellForRowAtIndexPathを呼び出さないものがあります。この場合を考慮する必要があります。

ただし、常にreloadData()のみを使用すると、安全で簡単です。


もう一つの方法は、コントローラクラスで物事をバック制御に関するコードを入れて、インデックスパスを取得するコールバックの閉鎖を経て、それを実行することです。

+0

ありがとうそれは私に役立ちます:) –

1

HERESに別の方法で、そのIndexPathプロパティを追加することができます:ここで

extension UITableViewCell { 

    var tableView: UITableView? { 
     return next(UITableView.self) 
    } 

    var indexPath: IndexPath? { 
     return tableView?.indexPath(for: self) 
    } 
} 

はあなたの例ではそれを使用することができるかです。ストーリーボードのテーブルビューセルのカスタムクラスを割り当てます。 rowAtIndexPathが呼び出されたときにIndexPath値を割り当てます。

class MyTableViewCell: UITableViewCell { 

    var indexPath: IndexPath? 
} 

custom class

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    var cell = tableView.dequeueReusableCell(withIdentifier: "cellid1", for: indexPath) 
    (cell as? MyTableViewCell)?.indexPath = indexPath 
    return cell 
} 
+0

ありがとうございます。それはうまく動作します –

+0

問題ない、うれしい誰かが私の答えをappreaciated :) –

0

私のソリューションは、サブクラス化されたのUITableViewCellを行う

@IBAction func likeBtnTapped(_ sender: AnyObject) { 

    //declare title of button 
    let title = sender.title(for: UIControlState()) 

    //I want get indexPath.row in here! 
    indexPath.flatMap { print($0) } 
} 
1

あなたのセル内にこれを試してみてください。 (スイフト4)

func getIndexPath() -> IndexPath { 
    guard let superView = self.superview as? UITableView else { 
    print("superview is not a UITableView - getIndexPath") 
    } 

    indexPath = superView.indexPath(for: self) 
    return indexPath 
} 
関連する問題