2015-12-02 3 views
0

То, что я пытаюсь добиться, - это когда ячейка в UITableview используется, она будет расширяться и отображать пользовательские ячейки1, другие ячейки останутся пользовательскими cell2. То, что я достиг до сих пор, - это расширение ячейки, но пользовательская ячейка не изменится.
После первоначального расследования я подумал, что значение currentRow не изменилось. Но потом я понял, что если он не изменился, строка не будет расширяться. Спасибо за помощь.
Вот код:Переключение пользовательских ячеек в UITableView в swift

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource { 

    var currentRow = 0 

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

     if indexPath.row == currentRow { 
      let cell:selectedWeatherViewCell = self.weatherCityTable.dequeueReusableCellWithIdentifier("selectedWeatherCell") as! selectedWeatherViewCell 
      cell.selectedCityLabels.text = city[indexPath.row] 
      cell.selectedTempLabels.text = tempertureF[indexPath.row] 
      cell.backgroundColor = UIColor.redColor() 
      return cell 
     } else { 
      let cell:cityWeatherViewCell = self.weatherCityTable.dequeueReusableCellWithIdentifier("cityWeatherViewCell") as! cityWeatherViewCell 
      cell.cityLabels.text = city[indexPath.row] 
      cell.tempLabels.text = tempertureF[indexPath.row] 
      cell.backgroundColor = UIColor.orangeColor() 
      return cell 
     } 
    } 

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     let selectedRowIndex = indexPath 
     currentRow = selectedRowIndex.row 

     tableView.beginUpdates() 
     tableView.endUpdates() 
    } 

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { 
     if indexPath.row == currentRow { 
      return 260 
     } else { 
      return 100 
     } 
    } 

} 

ответ

1

Вы должны перезагрузить пораженную строку (ы) для того, чтобы типа клеток изменения -

var currentRow:Int? 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    var reloadRows=[NSIndexPath]() 
    if self.currentRow != nil && indexPath.row != self.currentRow! { 
     reloadRows.append(NSIndexPath(forRow: self.currentRow!, inSection: indexPath.section)) 
    } 
    self.currentRow=indexPath.row 
    reloadRows.append(NSIndexPath(forRow: self.currentRow!, inSection: indexPath.section)) 
    tableView.reloadRowsAtIndexPaths(reloadRows, withRowAnimation: UITableViewRowAnimation.Automatic) 
    tableView.deselectRowAtIndexPath(indexPath, animated: true) 
} 
+0

Спасибо за информацию. что решает мою проблему. –