自定义单元格错误addTarget按钮

时间:2014-11-04 13:19:38

标签: ios swift tableview

我按如下方式创建了一个布局:在storyborad中,我有一个包含表视图的视图,并且我将类 TableViewController 关联起来。然后,我创建了一个带有自定义单元格的新视图,并为其分配了 CustomCell 类。

当我尝试为按钮指定动作时,我遇到了问题。由于各种原因,我需要在TableViewController中分配动作。

这是我的代码:

这是CustomCell:

class CustomCategoryTableViewCell:UITableViewCell {

    @IBOutlet weak var customButton: UIButton!
    @IBOutlet weak var title: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()

    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        println("Cell is selected.")

    }
}

这是我的TableViewController:

 override func viewDidLoad() {
            super.viewDidLoad()

            // Register custom cell
            var nib = UINib(nibName: "CategoryCell", bundle: nil)
            tableView.registerNib(nib, forCellReuseIdentifier: "CustomCellCategory")

      …more code
        }


        override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            println("numero rihe \(myArray.count)")
            return myArray.count
        }

        override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

            let cellCustom:CustomCategoryTableViewCell = tableView.dequeueReusableCellWithIdentifier("CustomCellCategory") as CustomCategoryTableViewCell

            // Button
            cellCustom.customButton.tag = indexPath.row

            println("current level: \(myArray[indexPath.row].level)")

            if (myArray[indexPath.row].level > 0){
             var puppy=""

             for (var i = 0; i < myArray[indexPath.row].level; i++){
                puppy += "  "
             }
                cellCustom.title.text = "\(puppy)-\(myArray[indexPath.row].nameCategory)"
            }else{
                cellCustom.title.text = myArray[indexPath.row].nameCategory
            }

            // Setto button aperto/chiuso
            if (myArray[indexPath.row].isOpen){

                var image = UIImage(named: "item-icon-folder.png");
                cellCustom.customButton.setBackgroundImage(image, forState: UIControlState.Normal)

            }else{

                var image = UIImage(named: "item-icon-folder-selected.png");
                cellCustom.customButton.setBackgroundImage(image, forState: UIControlState.Normal)
            }

            //cellCustom.customButton(cellCustom, action:"searchChild", forControlEvents: UIControlEvents.TouchUpInside)


            // Nascondo Button.
            if (!myArray[cellCustom.customButton.tag].hasChild){
                print("Non ha figli")
                cellCustom.customButton.hidden = true

                var image = UIImage(named: "item-counter.png");
                cellCustom.customButton.setBackgroundImage(image, forState: UIControlState.Normal)
            }
            cellCustom.customButton.addTarget(cellCustom, action: "buttonAction", forControlEvents: UIControlEvents.TouchUpInside)

            cellCustom.title.numberOfLines = 3
            cellCustom.title.lineBreakMode = .ByWordWrapping

            return cellCustom
        }


        func buttonAction(sender:UIButton!) {
    println(“tapped”)
    }

当我运行时,给我一个错误:

*** First throw call stack:
(
    0   CoreFoundation                      0x000000010c574f35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010e0b8bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010c57c04d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010c4d427c ___forwarding___ + 988
    4   CoreFoundation                      0x000000010c4d3e18 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x000000010ce018be -[UIApplication sendAction:to:from:forEvent:] + 75
    6   UIKit                               0x000000010cf08410 -[UIControl _sendActionsForEvents:withEvent:] + 467
    7   UIKit                               0x000000010cf077df -[UIControl touchesEnded:withEvent:] + 522
    8   UIKit                               0x000000010d1ae540 _UIGestureRecognizerUpdate + 9487
    9   UIKit                               0x000000010ce46ff6 -[UIWindow _sendGesturesForEvent:] + 1041
    10  UIKit                               0x000000010ce47c23 -[UIWindow sendEvent:] + 667
    11  UIKit                               0x000000010ce149b1 -[UIApplication sendEvent:] + 246
    12  UIKit                               0x000000010ce21a7d _UIApplicationHandleEventFromQueueEvent + 17370
    13  UIKit                               0x000000010cdfd103 _UIApplicationHandleEventQueue + 1961
    14  CoreFoundation                      0x000000010c4aa551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    15  CoreFoundation                      0x000000010c4a041d __CFRunLoopDoSources0 + 269
    16  CoreFoundation                      0x000000010c49fa54 __CFRunLoopRun + 868
    17  CoreFoundation                      0x000000010c49f486 CFRunLoopRunSpecific + 470
    18  GraphicsServices                    0x000000011066b9f0 GSEventRunModal + 161
    19  UIKit                               0x000000010ce00420 UIApplicationMain + 1282
    20  XMLParser-Tutorial                  0x000000010c24056e top_level_code + 78
    21  XMLParser-Tutorial                  0x000000010c2405aa main + 42
    22  libdyld.dylib                       0x000000010e892145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

2 个答案:

答案 0 :(得分:3)

这不起作用的原因是因为您将委托设置为“cellCustom”而不是视图控制器。 替换此行:

cellCustom.customButton.addTarget(cellCustom, action: "buttonAction", forControlEvents: UIControlEvents.TouchUpInside)

有了这个:

cellCustom.customButton.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)

答案 1 :(得分:0)

试试这个,

[2016-09-14 08:32:40] lumen.ERROR: InvalidArgumentException: Database 
[mysql] not 
configured. in /opt/vendor/illuminate/database/DatabaseManager.php:239
Stack trace:
#0 /opt/vendor/illuminate/database/DatabaseManager.php(158):     
Illuminate\Database\DatabaseManager->getConfig('mysql')
#1 /opt/vendor/illuminate/database/DatabaseManager.php(68):   
Illuminate\Database\DatabaseManager->makeConnection('mysql')
#2 /opt/vendor/illuminate/queue/Failed/DatabaseFailedJobProvider.php(110): 
Illuminate\Database\DatabaseManager->connection('mysql')
#3 /opt/vendor/illuminate/queue/Failed/DatabaseFailedJobProvider.php(58):    
Illuminate\Queue\Failed\DatabaseFailedJobProvider->getTable()
#4 /opt/vendor/illuminate/queue/Worker.php(313): 
Illuminate\Queue\Failed\DatabaseFailedJobProvider->log('redis', 'data-ingestion-...',    
'{"job":"Illumin...')
#5 /opt/vendor/illuminate/queue/Worker.php(204): Illuminate\Queue\Worker-
>logFailedJob('redis', Object(Illuminate\Queue\Jobs\RedisJob))
#6 /opt/vendor/illuminate/queue/Worker.php(156): Illuminate\Queue\Worker->process('redis',   
Object(Illuminate\Queue\Jobs\RedisJob), '1', 0)
#7 /opt/vendor/illuminate/queue/Worker.php(111): Illuminate\Queue\Worker->pop(NULL, 'data-   
ingestion-...', 0, 3, '1')
#8 /opt/vendor/illuminate/queue/Worker.php(85): Illuminate\Queue\Worker-
>runNextJobForDaemon(NULL, 'data-ingestion-...', 0, 3, '1')
#9 /opt/vendor/illuminate/queue/Console/WorkCommand.php(119): Illuminate\Queue\Worker-
>daemon(NULL, 'data-ingestion-...', 0, 128, 3, '1')
#10 /opt/vendor/illuminate/queue/Console/WorkCommand.php(78): 
Illuminate\Queue\Console\WorkCommand->runWorker(NULL, 'data-ingestion-...', 0, 128, true)