简体   繁体   English

tableView原型单元,在tableView外部带有操作按钮

[英]tableView prototype cell with action button outside tableView

I am getting started with mobile application development with Swift. 我正在开始使用Swift进行移动应用程序开发。 I designed a form within a viewController using tableView prototype cells (not using UITableViewController and static cells as I require to place other controls within the view controller). 我使用tableView原型单元(不使用UITableViewController和静态单元,因为我需要在视图控制器中放置其他控件)在viewController中设计表单。

Every cell has a unique identifier and designed using story board. 每个单元都有唯一的标识符,并使用故事板进行设计。 I have created a sub class for UITableViewCell and assigned the same to all the cells. 我为UITableViewCell创建了一个子类,并将其分配给所有单元格。

class VehicleTableViewCell: UITableViewCell {


@IBOutlet var txtModel: UITextField!
//@IBOutlet weak var txtModel: UITextField!
@IBOutlet weak var txtODO: UITextField!
@IBOutlet weak var txtFuel1: UITextField!
@IBOutlet weak var txtFuel2: UITextField!

@IBOutlet weak var lblDistanceUnit: UILabel!
@IBOutlet weak var lblFuel2Unit: UILabel!
@IBOutlet weak var lblFuel1Unit: UILabel!
@IBOutlet weak var lblMake: UILabel!
@IBOutlet weak var lblFuelType: UILabel!
@IBOutlet weak var lblCurrency: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}}

Here is my UIViewController that does the regular stuff: 这是我的UIViewController,可以执行常规操作:

class ViewController: UIViewController,  UITableViewDataSource, UITableViewDelegate,UISearchBarDelegate {

@IBOutlet weak var tableView: UITableView!

// Array of cell identifiers
let cellArray = ["CellMake", "CellModel", "CellODO", "CellFuelType", "CellFuel1", "CellFuel2", "CellCurrency"]


override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return cellArray.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
    let cell = tableView.dequeueReusableCellWithIdentifier(cellArray[indexPath.row]) as! VehicleTableViewCell
    if indexPath.row == 1
    {
        cell.txtModel.addTarget(self, action: "txtModelChanged:", forControlEvents: UIControlEvents.TouchUpInside)

        println("Inside index row 1")
    }

    return cell        
}

@IBAction func txtModelChanged(sender: UITextField!)
{
    println("test")
}}

Here are couple of things I'm confused and unable to proceed. 这是我很困惑且无法继续进行的几件事。

  1. I have added target for txtModel at cellForRowAtIndexPath and mapped it to function txtModelChanged on touchUpInside event. 我已经在cellForRowAtIndexPath添加了txtModel的目标,并在touchUpInside事件上将其映射到函数txtModelChanged。 Simply it didn't trigger the function nor it didn't throw any error. 只是它没有触发函数,也没有引发任何错误。 Any guess on what might be the issue? 关于可能是什么的任何猜测?

  2. I'm try to get values of all the objects on click of "Done" button. 我尝试单击“完成”按钮来获取所有对象的值。 How to traverse through the tableView to get all the values? 如何遍历tableView以获取所有值?

  3. Is there any other better way to achieve the same? 还有其他更好的方法可以达到相同目的吗?

First 第一

If you want to fire some method, when user touches your UITextField - you should override text field delegate method textFieldDidBeginEditing and call resign first responder to avoid the keyboard, and then write your custom code or method calls. 如果要触发某些方法,则当用户触摸UITextField时,应重写文本字段委托方法textFieldDidBeginEditing并调用第一响应者以避免键盘操作,然后编写自定义代码或方法调用。

Taken from: https://stackoverflow.com/a/4919134/2210926 摘自: https : //stackoverflow.com/a/4919134/2210926

In this particular question, it is not important whether you're using Swift or Obj-C. 在这个特定问题中,使用Swift还是Obj-C并不重要。

Second 第二

It's not an option, you have no guarantee that all your cell view will be alive at that moment. 这不是一种选择,您无法保证此时所有单元格视图都将保持活动状态。 You need to store all that data somewhere, like in an array for example. 您需要将所有数据存储在某个地方,例如在数组中。

For generic form designing, I found https://github.com/escoz/QuickDialog very helpful. 对于通用表单设计,我发现https://github.com/escoz/QuickDialog非常有用。 I have used it in one of my application. 我已经在我的一个应用程序中使用过它。 If you want to implement your own logic then I think this sample will help you to infer the information. 如果您想实现自己的逻辑,那么我认为该示例将帮助您推断信息。

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 280;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        NSString *CellIdentifier = @"Cell";
        CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
return cell;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM