我面临的问题是两行的子视图正在对仅用于一行的子视图的动作做出反应。

Image1

Image2

我上面发布的图像是仅按“醋酸盐”行中的星形子视图按钮的结果

以下是我的代码:

首先,对于UIView的子类,使按钮在触摸时充满。

import UIKit

class StarButton: UIView {

var order : Int = 0

override init (frame : CGRect)
{
    super.init(frame : frame)
    initStar()
}

required init?(coder aDecoder: NSCoder){
    super.init(coder: aDecoder)
    initStar()
}

func initStar(){

    let filledStarImage = UIImage(named: "filledStar")
    let emptyStarImage = UIImage(named: "emptyStar")

    let button = UIButton(frame: CGRect(x: 2, y: 2, width: 33, height: 33))

    button.userInteractionEnabled = true
    button.addTarget(self, action: #selector(StarButton.fillingStar(_:)), forControlEvents: UIControlEvents.TouchUpInside)

    let comparator = favoritesList.stringForKey("\(order)")

    if (comparator != nil){
        button.selected = true
    }

    button.setImage(emptyStarImage, forState: .Normal)
    button.setImage(filledStarImage, forState: .Selected)

    addSubview(button)
}


func fillingStar(sender: UIButton){
    if (sender.selected) == false{
        favoritesList.setObject(ChemQuizCollection.wordListObject.quizList[order], forKey: "\(order)")
        sender.selected = !sender.selected
        favoritesList.synchronize()
    } else{
        favoritesList.removeObjectForKey("\(order)")
        sender.selected = !sender.selected
        favoritesList.synchronize()
    }


}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
{
    for view in subviews
    {
        view.touchesBegan(touches, withEvent: event)
    }
}

override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool
{
    return true
}


}


其次,对于UITableViewController

class WordListTableViewController: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    for i in 0...25{
        favoritesList.setObject(nil, forKey: "\(i)")
    }
    favoritesList.synchronize()


    //setting the wordList of wordListObject


    ChemQuizCollection.wordListObject.quizList.sortInPlace()

    ChemQuizCollection.formulaImages.sortInPlace()

    tableView.estimatedRowHeight = 30

    // Uncomment the following line to preserve selection between presentations
    // self.clearsSelectionOnViewWillAppear = false

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source


override func numberOfSectionsInTableView(tableView: UITableView) -> Int {

    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 25
}


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

        let cell = self.tableView.dequeueReusableCellWithIdentifier("WordListTableCell", forIndexPath: indexPath) as! WordListTableViewCell

        let row = indexPath.row
        cell.starButtonView.order = row

        cell.formulaLabel.font = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)
        cell.formulaLabel.text = ChemQuizCollection.wordListObject.quizList[row]
        cell.formulaImage.image = UIImage(named: ChemQuizCollection.formulaImages[row])

        return cell
    }


我只是粘贴了我的大部分代码,这样才更有意义。请告诉我是否要进一步解释某些部分,或者是否应该对代码进行更多修剪以显示必要部分。

谢谢大家的帮助。 Stackoverflow是一个很棒的社区。

最佳答案

这是因为您的单元格已在cellForRowAtIndexPath中重用。因此,您必须更新您的StarButton状态。您可以执行以下操作:

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

        let cell = self.tableView.dequeueReusableCellWithIdentifier("WordListTableCell", forIndexPath: indexPath) as! WordListTableViewCell


// Here, you check if favoritesList contains the current item
// If yes, you set the button to enabled = true.
// If no, you set the button to enable = false


        return cell
    }

10-07 22:31