tableview中的多行对一行的子视图的一个动作做出反应

时间:2016-08-26 09:01:58

标签: ios swift uitableview uiview

我面临的问题是两行的子视图正在响应一个仅用于一行子视图的动作。

Image1

Image2

我上面发布的图片只是按下“Acetate”行中的星形子视图按钮的结果

以下是我的代码:

首先,对于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是一个非常棒的社区。

1 个答案:

答案 0 :(得分:0)

这是因为您的单元格正在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
    }