我很难在我的UIView中显示两个tableviews。
如果我隐藏第一个表格,它只会显示第二个表格。
有任何帮助吗?我需要让它显示两个表,一个在另一个之下。它只能在隐藏另一个时单独工作:(
class AccountsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
//MARK: Properties
@IBOutlet weak var totalLabel: UILabel!
@IBOutlet weak var tableview1: UITableView!
@IBOutlet weak var tableview2: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
//Set the table background as the image
tableview1.backgroundView = UIImageView(image: UIImage(named: "splasnowords-1.png"))
tableview2.backgroundView = UIImageView(image: UIImage(named: "splasnowords-1.png"))
//Use the edit button item provided by the table view controller
navigationItem.leftBarButtonItem = editButtonItem
//self.navigationItem.leftBarButtonItem = self.editButtonItem;
//Calculate the latest totalstandings
BudgetDataModel.calculateTotalStandings()
totalLabel.text = ("Total Current Standings = £\(BudgetDataModel.returnTrueValue(number: BudgetDataModel.total))")
self.tableview1.delegate = self
self.tableview2.delegate = self
self.tableview1.dataSource = self
self.tableview2.dataSource = self
self.tableview2.isHidden = false
self.tableview1.isHidden = true
// self.tableview.reloadData()
// self.tableview2.reloadData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidAppear(_ animated: Bool){
tableview1.reloadData()
tableview2.reloadData()
}
// MARK: - Table view data source
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
/*
if (tableView == self.tableview){
return 1
//BudgetDataModel.budgets.count
}
else if tableView == self.tableview2{
return 1
//SavingsDataModel.savings.count
}
else{
return 2
}*/
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?{
//reload data?
if (tableView == self.tableview1){
return "Budgets"
}
else{
return "Savings"
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var rowCount = 0
if (tableView == self.tableview1) {
rowCount = BudgetDataModel.budgets.count
}
if (tableView == self.tableview2) {
rowCount = SavingsDataModel.savings.count
}
return rowCount
// #warning Incomplete implementation, return the number of rows
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//Table view cells are reused and should be dequeued using a cell identifier.
if (tableView == self.tableview1){
let cellIdentifier = "AccountsTableViewCell"
let cell = self.tableview1.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! AccountsTableViewCell
let budget = BudgetDataModel.budgets[(indexPath as NSIndexPath).row]
cell.nameLabel.text = budget.name
cell.amountLabel.text = ("£\(BudgetDataModel.returnTrueValue(number: budget.amount))")
cell.backgroundColor = UIColor(white: 1, alpha: 0.5)
return cell
}
//Doesn't go into this if statement below
else {
//(tableView == self.tableview2)
let cellIdentifier2 = "SavingsTableViewCell"
let cell = self.tableview2.dequeueReusableCell(withIdentifier: cellIdentifier2, for: indexPath) as! SavingsTableViewCell
let saving = SavingsDataModel.savings[(indexPath as NSIndexPath).row]
cell.savingsnameLabel.text = saving.savingname
cell.savingsamountLabel.text = ("£\(BudgetDataModel.returnTrueValue(number: saving.savingamount))")
cell.backgroundColor = UIColor(white: 1, alpha: 0.5)
return cell
}
//return cell
}
/* else { preconditionFailure ("unexpected cell type") }
}*/
// Override to support conditional editing of the table view.
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
// Override to support editing the table view.
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
if (tableView == self.tableview1){
// Delete the row from the data source
BudgetDataModel.budgets.remove(at: indexPath.row)
BudgetDataModel.saveBudgets()
BudgetDataModel.calculateTotalStandings()
totalLabel.text = ("Total Current Standings = £\(BudgetDataModel.returnTrueValue(number:BudgetDataModel.total))")
// self.tableview.reloadData()
tableView.deleteRows(at: [indexPath], with: .fade)
}
else if (tableView == self.tableview2){
// Delete the row from the data source
SavingsDataModel.savings.remove(at: indexPath.row)
SavingsDataModel.saveSavings()
//implement BudgetDataModel.calculateTotalStandings()
//implement totalLabel.text = ("Total Current Standings = £\(BudgetDataModel.returnTrueValue(number:BudgetDataModel.total))")
//self.tableview2.reloadData()
tableView.deleteRows(at: [indexPath], with: .fade)
}
} else if editingStyle == .insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
// Override to support rearranging the table view.
func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
}
/*
// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return true
}
*/
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ShowDetail"{
let budgetDetailViewController = segue.destination as! BudgetViewController
//Get the cell that generated this segue.
if let selectedBudgetCell = sender as? AccountsTableViewCell {
let indexPath = tableview1.indexPath(for: selectedBudgetCell)!
let selectedBudget = BudgetDataModel.budgets[indexPath.row]
budgetDetailViewController.budget = selectedBudget
}
}
else if segue.identifier == "AddItem"{
//self.tableview.reloadData()
print("Adding new budget.")
}
else if segue.identifier == "ShowSavings"{
let savingDetailViewController = segue.destination as! SavingsViewController
//Get the cell that generated this segue.
if let selectedSavingsCell = sender as? SavingsTableViewCell {
let indexPath = tableview2.indexPath(for: selectedSavingsCell)!
let selectedSavings = SavingsDataModel.savings[indexPath.row]
savingDetailViewController.saving = selectedSavings
}
}
else if segue.identifier == "AddSaving"{
//self.tableview2.reloadData()
print ("Adding new saving.")
}
}
//MARK: Actions
@IBAction func unwindToBudgetList(_ sender: UIStoryboardSegue){
if let sourceViewController = sender.source as? BudgetViewController, let budget = sourceViewController.budget {
if let selectedIndexPath = tableview1.indexPathForSelectedRow{
//Update an existing budget.
BudgetDataModel.budgets[selectedIndexPath.row] = budget
self.tableview1.reloadRows(at: [selectedIndexPath], with: .none)
}
else{
//Add a new budget
let newIndexPath = IndexPath(row:BudgetDataModel.budgets.count, section: 0)
BudgetDataModel.budgets.append(budget)
self.tableview1.insertRows(at: [newIndexPath as IndexPath], with: .bottom)
}
//Save the budgets.
BudgetDataModel.saveBudgets()
BudgetDataModel.calculateTotalStandings()
totalLabel.text = ("Total Current Standings = £\(BudgetDataModel.returnTrueValue(number: BudgetDataModel.total))")
}
}
@IBAction func unwindtoSavingsList(_ sender: UIStoryboardSegue){
if let sourceViewController = sender.source as? SavingsViewController, let savings = sourceViewController.saving {
if let selectedIndexPath = tableview2.indexPathForSelectedRow{
//Update an existing budget.
SavingsDataModel.savings[selectedIndexPath.row] = savings
self.tableview2.reloadRows(at: [selectedIndexPath], with: .none)
}
else{
//Add a new saving
let newIndexPath = IndexPath(row:SavingsDataModel.savings.count, section: 0)
SavingsDataModel.savings.append(savings)
self.tableview2.insertRows(at: [newIndexPath as IndexPath], with: .bottom)
}
//Save the budgets.
SavingsDataModel.saveSavings()
//implement SavingsDataModel.calculateTotalStandings()
// totalLabel.text = ("Total Current Standings = £\(BudgetDataModel.returnTrueValue(number: BudgetDataModel.total))")
}
}
答案 0 :(得分:2)
答案 1 :(得分:2)
建议在视图中使用标准VC而不是TVC和2个tableviews。将每个连接到VC作为插座。将协议UITableViewDelegate,UITableViewDataSource添加到VC。然后,在IB中,控制从每个tableview拖动到VC头中最左边的图标,并连接Delegate和DataSource。最后,选择每个TableView并为其指定一个Tag值,以便在Tableview方法中区分它们。
答案 2 :(得分:0)
不幸的是,在我的情况下,这是由于我的约束,因为我已将它们放入堆栈视图,但堆栈视图未设置为正确分发。所以我需要在故事板中修改该设置。然后两者都出现并按预期正确。