ios - 在 Swift 的 UIViewController 中添加 UITableView 的问题

标签 ios uitableview swift uiviewcontroller

所以我尝试构建一个单屏应用程序,我从 UIViewController 开始。在其之上有一个 UITextfield、一个按钮和一个 UITableView。

从本质上讲,该应用程序的预期功能是让用户在 UITextField 中键入一个词,点击一个按钮,然后将其显示在 UITableView 上。

当按钮将 UITextField 条目附加到不同屏幕上的 UITableViewController 时,我在执行此操作时从来没有遇到过问题,但由于某种原因,我在 UIViewController 上嵌入的 UITableView 遇到了问题...在我的 Storyboard上我确实链接了UITableView 作为 UIViewController 的委托(delegate)和数据源,所以这不应该是问题所在。

有什么想法吗?我的代码贴在下面。

import UIKit

var groupList:[String] = []

class ExistingGroups: UIViewController,  UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource
{

@IBOutlet weak var addGroup: UITextField!


@IBAction func addGroupButton(sender: AnyObject) {

    self.view.endEditing(true)

    var error = ""

    if addGroup.text == "" {

        error = "Please enter a Group!"
    } else {

        groupList.append(addGroup.text)

    }

    if error != "" {

        var alert = UIAlertController(title:"Error In Form", message: error, preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title:"OK", style: .Default, handler: { action in

            self.dismissViewControllerAnimated(true, completion:nil)

        }))

        self.presentViewController(alert, animated:true, completion:nil)

    }

    addGroup.text = ""


}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    addGroup.placeholder = "Enter Group Name"
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func touchesBegan(touches:NSSet, withEvent event:UIEvent)
{
    super.touchesBegan(touches, withEvent: event)
    self.view.endEditing(true)

}

func textFieldShouldReturn(textField: UITextField!) -> Bool // called when 'return' key pressed. return NO to ignore.
{
    textField.resignFirstResponder()
    return true;
}

func tableView(tableView:UITableView, numberOfRowsInSection section: Int) -> Int {

    return groupList.count
}

func numberOfSectionsInTableView(tableView:UITableView) -> Int {

    return 1
}


func tableView(tableView:UITableView, cellForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCell {

    var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("groupcell") as UITableViewCell

    cell.textLabel?.text = groupList[indexPath.row]


    return cell
}



}

最佳答案

首先,以这种方式为您的 TableView 创建一个 IBOutlet:

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {

var groupList = [String]()
@IBOutlet weak var addGroup: UITextField!
@IBOutlet weak var tableView: UITableView!  //<<-- TableView Outlet

// Rest of your code..

}

之后在您的 viewDidLoad 方法中执行此操作:

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "groupcell")

    self.addGroup.delegate = self
    tableView.delegate = self
    tableView.dataSource = self
}

之后,一旦元素被添加到 groupList 中,您就可以重新加载您的 tableView。在这里你可以这样做:

@IBAction func addGroupButton(sender: AnyObject) {

    // Your Code

    } else {

        groupList.append(addGroup.text)
        self.tableView.reloadData()

    }

   //Your Code

}

并更新这个函数:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("groupcell", forIndexPath: indexPath) as UITableViewCell

    cell.textLabel.text = self.groupList[indexPath.row]
    return cell
}

你可以检查我为你创建的最终代码是 HERE .

关于ios - 在 Swift 的 UIViewController 中添加 UITableView 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27372595/

相关文章:

ios - 无法在单例类中初始化 CGFloat

ios - Objective C 中 UILocalNotification 的图标

uitableview - Swift:将 UITableViewCell 标签传递给新的 ViewController

swift - 如何在 Swift 中初始化 AVAudioPlayer

swift - 添加 Firebase 帖子会覆盖所有其他帖子 Swift

ios - UICollectionView didSelectItem 返回错误的索引路径

ios - NSMutableDictionary中的数字

ios - 为什么我的 UITableView 的顶部有额外的填充,在 iOS7 中具有 UITableViewStyleGrouped 样式

ios - 告诉 UITextField 在嵌入 UITableViewCell 时成为第一响应者

ios - UIImage 到 PHAsset