[英]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")
}}
這是我很困惑且無法繼續進行的幾件事。
我已經在cellForRowAtIndexPath添加了txtModel的目標,並在touchUpInside事件上將其映射到函數txtModelChanged。 只是它沒有觸發函數,也沒有引發任何錯誤。 關於可能是什么的任何猜測?
我嘗試單擊“完成”按鈕來獲取所有對象的值。 如何遍歷tableView以獲取所有值?
還有其他更好的方法可以達到相同目的嗎?
第一
如果要觸發某些方法,則當用戶觸摸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.