繁体   English   中英

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

[英]tableView prototype cell with action button outside tableView

我正在开始使用Swift进行移动应用程序开发。 我使用tableView原型单元(不使用UITableViewController和静态单元,因为我需要在视图控制器中放置其他控件)在viewController中设计表单。

每个单元都有唯一的标识符,并使用故事板进行设计。 我为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
}}

这是我的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")
}}

这是我很困惑且无法继续进行的几件事。

  1. 我已经在cellForRowAtIndexPath添加了txtModel的目标,并在touchUpInside事件上将其映射到函数txtModelChanged。 只是它没有触发函数,也没有引发任何错误。 关于可能是什么的任何猜测?

  2. 我尝试单击“完成”按钮来获取所有对象的值。 如何遍历tableView以获取所有值?

  3. 还有其他更好的方法可以达到相同目的吗?

第一

如果要触发某些方法,则当用户触摸UITextField时,应重写文本字段委托方法textFieldDidBeginEditing并调用第一响应者以避免键盘操作,然后编写自定义代码或方法调用。

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

在这个特定问题中,使用Swift还是Obj-C并不重要。

第二

这不是一种选择,您无法保证此时所有单元格视图都将保持活动状态。 您需要将所有数据存储在某个地方,例如在数组中。

对于通用表单设计,我发现https://github.com/escoz/QuickDialog非常有用。 我已经在我的一个应用程序中使用过它。 如果您想实现自己的逻辑,那么我认为该示例将帮助您推断信息。

-(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