gpt4 book ai didi

ios - Swift 3.1 UITableViewController 作为 subview 或弹出窗口

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:23:46 26 4
gpt4 key购买 nike

我有一个棘手的任务。我有带有两个 tableView、按钮等的 TableUIViewController(父 UIViewController)。此 UIViewController 显示有关咖啡馆中表的信息。

我还有另一个名为 MenuTableViewController 的 Controller (父级是 UITableViewController),它是显示菜单项列表的单独 View 。只有三个 UI 项目 - 每个单元格中有两个标签和按钮。

想法:我想以两种不同的方式使用这个 MenuTableViewController。

  1. 如果从主菜单转到此 View - 只显示菜单并允许用户编辑每个单元格。 Button.title = "打开"。

  2. 如果 View 是从 TableUIViewController 打开的 - 将按钮标题更改为“选择”,如果按下按钮创建订单等则返回 MenuTableViewController。

我发现 perform segue + prepare for segue 对我有用,但如果我使用 segue,我会遇到导航 Controller 的问题 - 我完全失去了“后退”按钮,无法返回主菜单。

所以我认为我可以使用 AddSubView 或一些弹出窗口执行以下步骤:

  1. 在 TableUIViewController 中点击按钮

  2. 显示 MenuTableViewController

  3. 点击某个按钮

  4. 返回 TableUIViewController - 理想情况下无需任何重新初始化,以保持所有变量及其值。

但我不明白如何在这里使用 AddSubview。有人可以指导我一下吗?或者提供一些例子。在这里没找到什么好东西。

这是我的类(class):

TableUIViewController

import UIKit
import CoreData

class TableUIViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, GuestAtTableTableViewCellDelegate, OrderInTableTableViewCellDelegate {
//MARK: variables:
//The following three variables will be set before segue to this view.
fileprivate let myGenericFunctions = MyGenericFunctions()
var tableName: String? = nil
var currentTable: TablesTable? = nil
var currentTableSession: TableSessionTable? = nil
let tableSessionTable = TableSessionTable()
let guestsTable = GuestsTable()
fileprivate var countOfGuests: Int {
get {
guard currentTableSession != nil else {return 0}
return guestsTable.getActiveGuestsForTable(tableSession: currentTableSession!)!.count
}
}
fileprivate var guestsTableFetchedResultsController: NSFetchedResultsController<GuestsTable>?
fileprivate var ordersTableFetchedResultsController: NSFetchedResultsController<OrdersTable>?


//MARK: IBOutlets
@IBOutlet weak var tableCapacityLabel: UILabel!
@IBOutlet weak var tableCountOfGuestsLabel: UILabel!
@IBOutlet weak var tableOpenTimeLabel: UILabel!
@IBOutlet weak var tableDescriptionTextView: UITextView!
@IBAction func closeTableButtonPressed(_ sender: UIButton) {
guard currentTableSession != nil else {return}
guestsTable.removeAllGuestsForTable(tableSession: currentTableSession!)
updateGuestsTableView()
updateOrdersTableView()
tableSessionTable.removeTableSession(tableSession: currentTableSession!)
currentTableSession = nil
updateLabels()
}
@IBOutlet weak var guestsTableView: UITableView!
@IBOutlet weak var ordersTableView: UITableView!

@IBAction func addGuestButtonPressed(_ sender: UIButton) {
if currentTableSession == nil {
currentTableSession = tableSessionTable.createTableSession(table: currentTable!)
}
if let capacity = Int(tableCapacityLabel.text!) {
guard capacity > countOfGuests else {return}
}

let guestsTable = GuestsTable()
guestsTable.addNewGuest(tableSession: currentTableSession!)
updateGuestsTableView()
updateLabels()
}
@IBAction func addOrderButtonPressed(_ sender: UIButton) {
guard currentTableSession != nil else {return}



}


//MARK: functions:
override func viewDidLoad() {
super.viewDidLoad()
guestsTableView.dataSource = self
guestsTableView.delegate = self
ordersTableView.dataSource = self
ordersTableView.delegate = self

updateGuestsTableView()
updateLabels()
}


private func updateLabels() {
tableCapacityLabel.text = String(describing: currentTable!.tableCapacity)
tableCountOfGuestsLabel.text = String(describing: countOfGuests)
if currentTableSession != nil {
tableOpenTimeLabel.text = String(describing: myGenericFunctions.convertDate(inputDate: currentTableSession!.openTime!))
} else {
tableOpenTimeLabel.text = " - "
}
if currentTable!.tableDescription != nil {
tableDescriptionTextView.text = currentTable!.tableDescription
}
}

//MARK: Delegates of cell buttons
func didPressGuestCellButton(guest: GuestsTable) {
guestsTable.closeGuest(guest: guest)
updateLabels()
updateGuestsTableView()
}

func didPressOrderCellButton(order: OrdersTable) {

}

//MARK: Functions for tableViews update
private func updateGuestsTableView () {
guard currentTableSession != nil else {return}
let tableView = guestsTableView
let context = AppDelegate.viewContext
let request : NSFetchRequest<GuestsTable> = GuestsTable.fetchRequest()
request.predicate = NSPredicate(format: "table= %@", currentTableSession!)
request.sortDescriptors = [NSSortDescriptor(key: "openTime", ascending: true, selector: #selector(NSString.localizedCaseInsensitiveCompare(_:)))]
guestsTableFetchedResultsController = NSFetchedResultsController<GuestsTable>(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
try? guestsTableFetchedResultsController?.performFetch()
tableView?.reloadData()
}

private func updateOrdersTableView () {
let tableView = ordersTableView
let context = AppDelegate.viewContext
let request : NSFetchRequest<OrdersTable> = OrdersTable.fetchRequest()
request.sortDescriptors = [NSSortDescriptor(key: "menuItem", ascending: true, selector: #selector(NSString.localizedCaseInsensitiveCompare(_:)))]
ordersTableFetchedResultsController = NSFetchedResultsController<OrdersTable>(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
try? ordersTableFetchedResultsController?.performFetch()
tableView?.reloadData()
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == self.guestsTableView {
let cell = tableView.dequeueReusableCell(withIdentifier: "guestCell", for: indexPath) as! GuestAtTableTableViewCell
if let guest = guestsTableFetchedResultsController?.object(at: indexPath) {
cell.guestNameLabel.text = guest.guestName
cell.openTimeLabel.text = "Пришел: " + myGenericFunctions.convertDate(inputDate: guest.openTime!)
if let closeTime = guest.closeTime {
cell.closeTimeLabel.text = "Ушел: " + myGenericFunctions.convertDate(inputDate: closeTime)
cell.closeGuestButton.isEnabled = false
cell.guestNameLabel.textColor = UIColor.darkGray
cell.openTimeLabel.textColor = UIColor.darkGray
cell.closeTimeLabel.textColor = UIColor.darkGray
}
cell.cellDelegate = self
cell.guest = guest
}
return cell
}
else {
let cell = tableView.dequeueReusableCell(withIdentifier: "orderCell", for: indexPath)
return cell
}
}

func numberOfSections(in tableView: UITableView) -> Int {
if tableView == self.guestsTableView {
return guestsTableFetchedResultsController?.sections?.count ?? 1
}
else if tableView == self.ordersTableView {
return ordersTableFetchedResultsController?.sections?.count ?? 1
}
else {return 1}
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == self.guestsTableView {
if let sections = guestsTableFetchedResultsController?.sections, sections.count > 0 {
return sections[section].numberOfObjects
}
else {
return 0
}
}
else if tableView == self.ordersTableView {
if let sections = ordersTableFetchedResultsController?.sections, sections.count > 0 {
return sections[section].numberOfObjects
}
else {
return 0
}
}
else {return 0}
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if tableView == self.guestsTableView {
if let sections = guestsTableFetchedResultsController?.sections, sections.count > 0 {
return sections[section].name
}
else {
return nil
}
}
else if tableView == self.ordersTableView {
if let sections = ordersTableFetchedResultsController?.sections, sections.count > 0 {
return sections[section].name
}
else {
return nil
}
}
else {return nil}

}

func sectionIndexTitles(for tableView: UITableView) -> [String]? {
if tableView == guestsTableView {
return guestsTableFetchedResultsController?.sectionIndexTitles
}
else {
return ordersTableFetchedResultsController?.sectionIndexTitles
}
}

func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
if tableView == guestsTableView {
return guestsTableFetchedResultsController?.section(forSectionIndexTitle: title, at: index) ?? 0
}
else if tableView == ordersTableView {
return ordersTableFetchedResultsController?.section(forSectionIndexTitle: title, at: index) ?? 0
}
else {return 0}
}

//Prepare for segues
/*override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "openMenuToAddOrder" {
if let menuTVC = segue.destination as? MenuTableViewController {
menuTVC.isOpenedFromTable = true
menuTVC.currentTableSession = currentTableSession
menuTVC.currentTable = currentTable
}
}
}*/
}

ManuTableViewController:

class MenuTableViewController: FetchedResultsTableViewController, MenuTableViewCellDelegate {
var tableName: String? = nil
var currentTable: TablesTable? = nil
var currentTableSession: TableSessionTable? = nil
var isOpenedFromTable: Bool = false
let ordersTable = OrdersTable()
fileprivate var fetchedResultsController: NSFetchedResultsController<MenuTable>?

override func viewDidLoad() {
updateMenuTableView()
self.navigationItem.rightBarButtonItem = self.editButtonItem
}

//MARK: delegate of table cell
func didPressMenuItemCellButton (menuItem: MenuTable) {
if isOpenedFromTable {
ordersTable.addOrUpdateOrderForTableSession(tableSession: currentTableSession!, menuItem: menuItem)
performSegue(withIdentifier: "returnToTableView", sender: self)
} else {
//here will be code for editing menu item
}
}

//MARK: Functioms for table view update
private func updateMenuTableView () {
let context = AppDelegate.viewContext
let request : NSFetchRequest<MenuTable> = MenuTable.fetchRequest()
request.sortDescriptors = [NSSortDescriptor(key: "itemName", ascending: true, selector: #selector(NSString.localizedCaseInsensitiveCompare(_:)))]
fetchedResultsController = NSFetchedResultsController<MenuTable>(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
fetchedResultsController?.delegate = self
try? fetchedResultsController?.performFetch()
tableView.reloadData()
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "menuCell", for: indexPath) as! MenuTableViewCell
if let menuTable = fetchedResultsController?.object(at: indexPath) {
cell.menuItemNameLabel.text = menuTable.itemName
cell.menuItemDescriptionLabel.text = menuTable.itemDescription
cell.menuItemPriceLabel.text = String(describing: menuTable.itemPrice)
if isOpenedFromTable == true {
cell.button.setTitle("Выбрать", for: UIControlState.normal)
} else {
cell.button.setTitle("Открыть", for: UIControlState.normal)
}
cell.menuItem = menuTable
cell.cellDelegate = self
}
return cell
}
//Prepare for segues
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "returnToTableView" {
if let tableTVC = segue.destination as? TableUIViewController {
tableTVC.currentTableSession = currentTableSession
tableTVC.currentTable = currentTable
tableTVC.tableName = currentTable?.tableName
}
}
}
}

extension MenuTableViewController {
override func numberOfSections(in tableView: UITableView) -> Int {
return fetchedResultsController?.sections?.count ?? 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let sections = fetchedResultsController?.sections, sections.count > 0 {
return sections[section].numberOfObjects
}
else {
return 0
}
}

override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
if let sections = fetchedResultsController?.sections, sections.count > 0 {
return sections[section].name
}
else {
return nil
}
}

override func sectionIndexTitles(for tableView: UITableView) -> [String]? {
return fetchedResultsController?.sectionIndexTitles
}

override func tableView(_ tableView: UITableView, sectionForSectionIndexTitle title: String, at index: Int) -> Int {
return fetchedResultsController?.section(forSectionIndexTitle: title, at: index) ?? 0
}
}

更新:这是我的 Storyboard。我有一种感觉,我没有正确使用导航 Controller 。但是在 View 之间没有一些 Controller - 后退按钮不会出现。对不起图片中的俄语文字。 enter image description here

标记为已解决后更新:

我已经删除了 performSegue 并将 controller 中的 segues 放到按钮本身。现在 Navigation controller 保留了 Back 按钮。还将 Update UI 函数移动到 viewWillAppear 以在 segues 之后保持表更新。

override func viewWillAppear(_ animated: Bool) {
updateGuestsTableView()
updateOrdersTableView()
updateLabels()
updateOrdersTableView()
}

另一个更新,现在它完全符合我的要求。我已经添加到由 Menu Cell Button 调用的函数以返回到之前的 ViewController(与 Back 按钮一样):

    //MARK: delegate of table cell
func didPressMenuItemCellButton (menuItem: MenuTable) {
if isOpenedFromTable {
ordersTable.addOrUpdateOrderForTableSession(tableSession: currentTableSession!,

menuItem: menuItem)
_ = navigationController?.popViewController(animated: true)
} else {
//here will be code for editing menu item
}
}

最佳答案

听起来你想要这样的结构:

enter image description here

如果您从主菜单中选择“编辑食物菜单”,您将直接进入“食物菜单”。如果您选择“接单”,您将看到“表格列表”。

如果您选择“编辑食物菜单”,那么您的食物菜单代码应显示“编辑”作为表格中每一行的按钮标题。点击“编辑”将带您进入“编辑菜单项” View 。

如果您选择“接受订单”,然后从列表中选择一张 table ,会将您带到“食物菜单”,但在这种情况下,您的食物菜单代码应显示“订单"作为按钮标题。点击“订购”会将食品添加到您的数据结构中,或者可能会显示确认警报,或者在此时执行任何其他需要的操作。

因为所有 View 都是同一导航 Controller 结构的一部分,所以您始终可以通过选择导航栏上的标准“<返回”按钮来“返回堆栈”。

希望这是有道理的:)

关于ios - Swift 3.1 UITableViewController 作为 subview 或弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44309653/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com