簡體   English   中英

在同一ViewController中由第二個UITableView控制的動態UITableView-Swift

[英]Dynamic UITableView controlled by second UITableView in same ViewController - Swift

這是我昨天提出的一個問題的跟進,我認為我已經根據收到的答案找到了解決方案,但是我遇到了新的困難。

我有一個帶有兩個UITableViewsPropertyTypeListPropertyDetailList )的ViewController ,我本質上想將其視為一個主詳細信息視圖,當選擇了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 {
    }

}

只需調用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 {
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM