2016-01-11 2 views
1

Я писал небольшое приложение Swift. Теперь я использую tableview и пытаюсь выбрать строку, изменить изображение и отменить выбор строки.deselectRowAtIndexPath ничего не делает

Мой заказ UITableViewCell класс выглядит следующим образом:

class SelectFriendTableViewCell: UITableViewCell { 

@IBOutlet weak var friendNameLabel: UILabel! 

@IBOutlet weak var friendSelectedImage: UIImageView! 

override func awakeFromNib() { 
    super.awakeFromNib() 

} 

override func setSelected(selected: Bool, animated: Bool) { 
    super.setSelected(selected, animated: animated) 

    // Configure the view for the selected state 
} 

И мой TableViewController имеет следующие функции:

здания клетки:

//Build the cell 
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCellWithIdentifier("SelectFriend", forIndexPath: indexPath) as! SelectFriendTableViewCell 

    //Default image is unselected 
    cell.friendSelectedImage.image = UIImage(named: "unselected") 
    //Get the label text 
    cell.friendNameLabel.text = friendList[indexPath.row] 

    return cell 
} 

ли материал при выборе ячейки (didSelectRowAtIndexPath)

//Cell was selected 
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    let cell = tableView.dequeueReusableCellWithIdentifier("SelectFriend", forIndexPath: indexPath) as! SelectFriendTableViewCell 

    //Check wheter the cell is or not selected 
    if(cell.friendSelectedImage.image == UIImage(named: "unselected")) 
    { 
     //if it's unselected, select it and add the user to the invited list 
     cell.friendSelectedImage.image = UIImage(named: "selected") 
     //Add to the invited list, the label can't be null 
     invitedList += [cell.friendNameLabel.text!] 
    } 

    else if(cell.friendSelectedImage.image == UIImage(named: "selected")) 
    { 
     //if it's selected, unselect it and remove the user from the invited list 
     cell.friendSelectedImage.image = UIImage(named: "unselected") 
     //Find the invited in the array and remove it 
     for(var i=0; i < invitedList.count; i++) 
     { 
      if(invitedList[i] == cell.friendNameLabel.text!) 
      { 
       invitedList.removeAtIndex(i) 
       break 
      } 
     } 
    } 
    else 
    { 
     print("error geting image") 
    } 

    //Trying to deselect the row, doesn't seem to be working 
    tableView.deselectRowAtIndexPath(indexPath, animated: false) 
} 

Что я вижу:

Didn't select any cells

Cell was selected

Там же проблема, что исчезает метка.

+1

Что вы хотите 'deselect' делать? – Tim

ответ

4

dequeueReusableCellWithIdentifier не получил предыдущую ячейку, вы должны использовать cellForRowAtIndexPath

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    //get the cell, 
    let cell = tableView.cellForRowAtIndexPath(indexPath) as! SelectFriendTableViewCell 
    //do some work 
} 
+1

Не просто отправляйте код без объяснения причин. Обновите свой ответ, объяснив, что было не так, и как ваш ответ решает проблему. – rmaddy

+0

Спасибо! Это сработало :) Итак? Я снова строю ячейку? Что тут происходит? –

+0

'cellForRowAtIndexPath' использовать для создания ячейки и' cellForRowAtIndexPath' использовать для доступа к ячейке. более подробно вы можете просмотреть [AppleDoc] (https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/index.html#//apple_ref/doc/uid/TP40006943-CH3-SW64) – Tom

Смежные вопросы