ios - TableView 中的多行对针对一行 subview 的一项操作使用react

标签 ios swift uitableview uiview

我面临的问题是两行的 subview 对仅一行 subview 的操作使用react。

Image1

Image2

我上面发布的图像是仅按“Acetate”行中的星形 subview 按钮的结果

以下是我的代码:

首先,对于 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
    }

关于ios - TableView 中的多行对针对一行 subview 的一项操作使用react,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39162407/

相关文章:

iphone - 在表格 View 中使用附件复选标记

ios - iPhone 应用程序中的 VOIP 实现

ios - UITableView 设置

ios - 如何自动调用 UITableViewDelegate 和 UITableViewDataSource 方法?

ios - 连续的 UIAlertControllers - IOS - Swift

iphone - UIPickerView 中的多行选择

ios - 如何避免 iOS 中的重复通知?

ios - 如何正确切换到不同的 ViewController

swift - 在 .forEach 闭包 Swift 中使用 .sortInPlace

ios - 如何从 TableView Controller 转到详细 Controller 并返回