LoginSignup
5
5

More than 5 years have passed since last update.

【Swift】UITableView上のカスタムボタンにタグ付け

Last updated at Posted at 2014-12-01

アプリ開発を始めて,ついでにQiita初投稿.

TableViewのアクセサリーボタンにタグ付け成功.
1000の位はセクション,1の位はセルの行を表しています.

TestTableView.swift

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var _myTableView: UITableView!
    let SECTION_NAME = ["Clip", "History"]
    var _clipItemArray = ["iOS8", "iOS7", "iOS6", "iOS5", "iOS4"]
    var _historyItemArray = ["5.x", "4.x", "2.x", "1.x"]

//略

    // セルの内容
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("MyCell", forIndexPath: indexPath) as UITableViewCell
        let button = UIButton(frame: CGRectMake(0, 0, BUTTON_SIZE, BUTTON_SIZE))
        button.setImage(UIImage(named: "Icon180.png"), forState: .Normal)
        button.addTarget(self, action: "tmpButtonTouch:", forControlEvents: UIControlEvents.TouchUpInside)
        button.tag = indexPath.section*1000+indexPath.row
        cell.accessoryView = button
        if indexPath.section == 0 {
            cell.textLabel!.text = "\(_recordItemArray[indexPath.row])"
        } else if indexPath.section == 1 {
            cell.textLabel!.text = "\(_historyItemArray[indexPath.row])"
        }
        return cell
    }

    func tmpButtonTouch(sender: UIButton!) {
        var button: UIButton = sender
        println("button.tag: \(button.tag)")
    }

http://lab.dolice.net/blog/2013/03/21/objc-button-args/

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5