2015-05-06 4 views
0

Это вопрос к вопросу, который я задал вчера, я думал, что у меня есть решение, основанное на полученном ответе, но у меня возникла новая проблема.Динамический UITableView контролируется вторым UITableView в том же ViewController - Swift

У меня есть один ViewController с двумя UITableViews (PropertyTypeList и PropertyDetailList) я принципиально хочу, чтобы относиться к этому, как Master Detail View, Когда строка PropertyTypeList выбирают обновления PropertyDetailList в массив на основе этого выбора. (Мне нужно, чтобы он находился в одном ViewController)

Проблема заключалась в том, что я не смог получить строку, выбранную из didSelectRowAtIndexPath, в cellForRowAtIndexPath, где я заполняю таблицы через инструкцию If, чтобы определить, какое представление таблицы находится в игре.

Теперь я получаю выбранную строку по переменной (currentRowInTypeList) и используя это значение для переключения, какой массив загружен в PropertyDetailList.

Несмотря на то, что currentRowInTypeList обновляется при каждом нажатии таблицы, нет. Что мне не хватает?

мой код следующим образом:

@IBOutlet weak var propertyTypeList: UITableView! 
@IBOutlet weak var propertyDetailList: UITableView! 

var testarray = ["test1", "test2"] 
var testarray2 = ["test3", "test4"] 
var currentRowInTypeList: Int = 0 

func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int 
{ 
    if tableView == propertyTypeList { 
     return projectSources.count; 
    } 
    else { 
     if currentRowInTypeList == 0 { 
      return testarray.count 
     } else { 
      return testarray2.count 
     } 
    } 
} 



func tableView(tableView: UITableView!, 
    cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! 
{ 
    let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier:"Cell") 

    if tableView == propertyTypeList { 
     cell.textLabel?.text = projectSources[indexPath.row] 
     return cell 
    } 
    else 
    { 
     if currentRowInTypeList == 0 { 
      cell.textLabel?.text = testarray[indexPath.row] 
      return cell 
     } else { 
     cell.textLabel?.text = testarray2[indexPath.row] 
     return cell 
     } 
    } 
} 

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
    tableView.deselectRowAtIndexPath(indexPath, animated: true) 
    let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier:"Cell") 

    if tableView == propertyTypeList { 
    currentRowInTypeList = indexPath.row 
    println(currentRowInTypeList) 
    } else { 
    } 

} 

ответ

1

Просто позвоните propertyDetailList.reloadData()

В коде:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
tableView.deselectRowAtIndexPath(indexPath, animated: true) 
let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier:"Cell") 

if tableView == propertyTypeList { 
    currentRowInTypeList = indexPath.row 
    propertyDetailList.reloadData() 
    println(currentRowInTypeList) 
} else { 
} 

}

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