BlueRad
BlueRad

Reputation: 109

Dynamic UITableView controlled by second UITableView in same ViewController - Swift

This is a followup to a question I asked yesterday, I thought I had a solution based upon the answer received, but I'm having a new difficulty.

I have a single ViewController with two UITableViews (PropertyTypeListand PropertyDetailList) I essentially want to treat this like a Master Detail View, When a row in PropertyTypeList is selected PropertyDetailList updates to an array based upon that selection. (I need it to be in one ViewController)

The problem was that I couldn't get the row selected from didSelectRowAtIndexPath into cellForRowAtIndexPath where I populate the tables through an If Statement to determine which tableview is in play.

I am now getting the currently selected row through a variable (currentRowInTypeList) and using that value to switch which array is loaded into PropertyDetailList.

Even though currentRowInTypeList is updating with each click the table isn't. What am I missing?

my code follows:

@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 {
    }

}

Upvotes: 0

Views: 153

Answers (1)

Leo
Leo

Reputation: 24714

Just call propertyDetailList.reloadData()

In your code:

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 {
}

}

Upvotes: 1

Related Questions