2016-05-27 7 views
0

すべてのセルにチェックボックスボタン付きのUITableViewを使用しています。ユーザーがそのセルまたはボタンをタップすると、そのボタンの画像が「選択された」画像に変更されなければなりません。しかし、私が任意のセルやボタンをタップすると、イメージは変化しません。UItableviewcellボタンの画像を変更できません

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

    let cell : categoryCell = tableView.dequeueReusableCellWithIdentifier("categoryCell") as! categoryCell 
    let dic : NSDictionary = categoryitem.objectAtIndex(indexPath.row) as! NSDictionary 
    cell.category_name.text = dic.valueForKey("category_name") as? String 


    return cell 

} 


func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return categoryitem.count 
} 
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    let cell : categoryCell = tableView.dequeueReusableCellWithIdentifier("categoryCell") as! categoryCell 

    cell.category_button.setImage(UIImage(named: "check_checkbox"), forState: UIControlState.Normal) 
    let dic : NSDictionary = categoryitem.objectAtIndex(indexPath.row) as! NSDictionary 
    print(dic.valueForKey("category_id") as! String) 
} 
+0

categoryCellにimageViewのIBOutletを作成しましたか? –

+0

しかし、私はuibuttonの画像を変更したい。だから私はuibuttonのiboutletを@SandeepBhandariを作成する –

+0

申し訳ありませんがUIButtonを意味した:D –

答えて

1

の変更のような

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 

    let cell : categoryCell = tableView.cellForRowAtIndexPath(indexPath) as! categoryCell 

    cell.category_button.setImage(UIImage(named: "check_checkbox"), forState: UIControlState.Normal) 
    let dic : NSDictionary = categoryitem.objectAtIndex(indexPath.row) as! NSDictionary 
    print(dic.valueForKey("category_id") as! String) 
} 

間違いなどdidSelectRowAtIndexPathであなたのコードは、あなたはあなたの再利用のためのセルを返します

let cell : categoryCell = tableView.dequeueReusableCellWithIdentifier("categoryCell") as! categoryCell 

をしましたが、あなたは、現在ロードされているセルにアクセスする必要がありますindexPathでユーザーがタップしているので:) cellForRowAtIndexPathを使用します。 cellForRowAtIndexPathは、現在ロードされているセルをindexPathに返します。

+0

感謝の男その時間savier。私のために働く。 –

+0

@ Jack.Right:いつも歓迎バディ:) –

0

あなたが戻ってあなたが画像を変更することができ、ボタンの呼び出しでcellForRowAtIndexPathメソッド であなたのボタン、チェックボックスにターゲットを追加する必要があります。

は、ここに私のテーブルコードです。この

[cell.checkButton addTarget:self action:@selector(buttonclick:) forControlEvents:UIControlEventTouchUpInside]; 
checkbutton.tag=indexpath.row; 
-(void)buttonclick:(id)sender 
{` 
// you can change your image here.. 
} 
+0

しかし、どのように私はその機能のボタンにアクセスできますか? –

+0

どのように私はセルの外のメソッドを定義できますか? –

+0

@ Jack.Right Sender – user3306145

関連する問題