2016-08-26 11 views
0

私が直面している問題は、2行のサブビューが1行のサブビューを対象とするアクションに反応していることです。tableviewの複数の行が1つの行のサブビューを対象とする1つのアクションに反応する

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は素晴らしいコミュニティです。

答えて

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 
    } 
+0

ありがとうございました。私が今問題にしているのは、実際のUIButtonであるStarButtonのサブビューにどのようにアクセスするかです。 StarButtonはUIViewのサブクラスでUIButtonサブビュー(私のテーブルの星型アイコン)を持っているので、そのUIButtonサブビューにアクセスする必要がありますが、どうやって見つけることができません。 しよう: cell.starButtonView.subviewsでサブビューのために{= subview.selected が真の は} はUIViewのは、「選択」メンバーを持っていないというエラーがスローされます。また – Daniel

+0

、私はなぜ、ボタンの状態を更新する必要があり得ることはありませんお気に入りリストの現在のアイテムによって異なります。ユーザのタッチに応じて、ボタンは塗りつぶしたり空になります。ユーザーが触れると状態が変わります。 お気に入りリストが存在するため、ユーザーがボタンをタッチすると、その行に関連付けられたアイテムがローカルにデバイスに保存されます。 – Daniel

関連する問題