UItableview中的相同类型的文本字段

时间:2016-12-02 05:44:28

标签: ios objective-c uitextfield

我有一个自定义uitableview单元格的tableview包含标签和文本字段。行数取决于来自API的值。所有文本文件都将以数字类型显示。所以我的问题是如何调用textfield确实输入并且textfield应该返回以及它将如何工作?

文本字段开始编辑

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField//Should begin editing
{
_tblVw_TargetSetting.contentInset =  UIEdgeInsetsMake(0, 0, 244, 0);

NSIndexPath *sibling = [NSIndexPath indexPathForRow:textField.tag inSection:0];

TargetSettingTableViewCell *cell = [_tblVw_TargetSetting cellForRowAtIndexPath:sibling];

NSIndexPath *indexPath = [_tblVw_TargetSetting indexPathForCell:cell];

for (NSInteger i = indexPath.row; i<[arrTargetSettingList count];) 
{
NSLog(@"%ld",(long)i);

if(i==[arrTargetSettingList count]-1)
{

NSIndexPath *sibling = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];

TargetSettingTableViewCell *mCell = [_tblVw_TargetSetting cellForRowAtIndexPath:sibling];

[mCell.txtFld_TargetSetting resignFirstResponder];

UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, FULLWIDTH, 50)];

numberToolbar.barStyle = UIBarStyleBlackTranslucent;

numberToolbar.items = [NSArray arrayWithObjects: [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)], [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(DoneWithNumberPad)],
                                       nil];

[numberToolbar sizeToFit];

valNumberPad = i;

mCell.txtFld_TargetSetting.inputAccessoryView = numberToolbar;

}

else{

if(i==0){

NSIndexPath *sibling = [NSIndexPath indexPathForRow:indexPath.row inSection:indexPath.section];

TargetSettingTableViewCell *mCell = [_tblVw_TargetSetting cellForRowAtIndexPath:sibling];

UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, FULLWIDTH, 50)];

numberToolbar.barStyle = UIBarStyleBlackTranslucent;

numberToolbar.items = [NSArray arrayWithObjects:[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],[[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStyleDone target:self action:@selector(nextWithNumberPad)],
                                           nil];

[numberToolbar sizeToFit];

valNumberPad = i;

mCell.txtFld_TargetSetting.inputAccessoryView = numberToolbar;

}

else{

NSIndexPath *sibling = [NSIndexPath indexPathForRow:indexPath.row-1 
inSection:indexPath.section];

TargetSettingTableViewCell *mCell = [_tblVw_TargetSetting cellForRowAtIndexPath:sibling];

[mCell.txtFld_TargetSetting resignFirstResponder];

UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, FULLWIDTH, 50)];
    numberToolbar.barStyle = UIBarStyleBlackTranslucent;

    numberToolbar.items = [NSArray arrayWithObjects: [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],[[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStyleDone target:self action:@selector(nextWithNumberPad)], nil];[numberToolbar sizeToFit];

valNumberPad = i;

mCell.txtFld_TargetSetting.inputAccessoryView = numberToolbar;

}

}

break;
}

return YES;

}

textfield应该返回

- (BOOL)textFieldShouldReturn:(UITextField *)textField//should return
{

NSIndexPath *sibling = [NSIndexPath indexPathForRow:textField.tag inSection:0];

TargetSettingTableViewCell *cell = [_tblVw_TargetSetting cellForRowAtIndexPath:sibling];

NSIndexPath *indexPath = [_tblVw_TargetSetting indexPathForCell:cell];
for (NSInteger i = indexPath.row; i<[arrTargetSettingList count];) {

TargetSettingTableViewCell *mAddressDetailsCell = [_tblVw_TargetSetting cellForRowAtIndexPath:indexPath];

[mAddressDetailsCell.txtFld_TargetSetting resignFirstResponder];

NSIndexPath *sibling = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section];

TargetSettingTableViewCell *cell = [_tblVw_TargetSetting cellForRowAtIndexPath:sibling];

[cell.txtFld_TargetSetting becomeFirstResponder];

break;

}

return YES;

}
> Toolbar Button Tap Methods

-(void)cancelNumberPad{

NSIndexPath *sibling = [NSIndexPath indexPathForRow:valNumberPad inSection:0];

TargetSettingTableViewCell *cell = [_tblVw_TargetSetting cellForRowAtIndexPath:sibling];

NSIndexPath *indexPath = [_tblVw_TargetSetting indexPathForCell:cell];

TargetSettingTableViewCell *mAddressDetailsCell = [_tblVw_TargetSetting cellForRowAtIndexPath:indexPath];

[mAddressDetailsCell.txtFld_TargetSetting resignFirstResponder];

[UIView beginAnimations:nil context:nil];

[UIView setAnimationDuration:0.2];

[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

CGRect frame = self.tblVw_TargetSetting.frame;

self.tblVw_TargetSetting.frame = CGRectMake(0.0f, 223.0f, frame.size.width, frame.size.height);

_tblVw_TargetSetting.contentInset =  UIEdgeInsetsMake(0, 0, 0, 0);
 [UIView commitAnimations];

}

-(void)nextWithNumberPad{

NSIndexPath *sibling = [NSIndexPath indexPathForRow:valNumberPad inSection:0];

TargetSettingTableViewCell *cell = [_tblVw_TargetSetting cellForRowAtIndexPath:sibling];

NSIndexPath *indexPath = [_tblVw_TargetSetting indexPathForCell:cell];        

for (NSInteger i = valNumberPad; i<[arrTargetSettingList count];) {

TargetSettingTableViewCell *mAddressDetailsCell = [_tblVw_TargetSetting cellForRowAtIndexPath:indexPath];

[mAddressDetailsCell.txtFld_TargetSetting resignFirstResponder];            

NSIndexPath *sibling = [NSIndexPath indexPathForRow:i+1 inSection:indexPath.section];

TargetSettingTableViewCell *cell = [_tblVw_TargetSetting cellForRowAtIndexPath:sibling];

[cell.txtFld_TargetSetting becomeFirstResponder];

break;

}

}

-(void)DoneWithNumberPad{

NSIndexPath *sibling = [NSIndexPath indexPathForRow:valNumberPad inSection:0];

TargetSettingTableViewCell *cell = [_tblVw_TargetSetting cellForRowAtIndexPath:sibling];

NSIndexPath *indexPath = [_tblVw_TargetSetting indexPathForCell:cell];

TargetSettingTableViewCell *mAddressDetailsCell = [_tblVw_TargetSetting cellForRowAtIndexPath:indexPath];

[mAddressDetailsCell.txtFld_TargetSetting resignFirstResponder];        

[UIView beginAnimations:nil context:nil];

[UIView setAnimationDuration:0.2];

[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

CGRect frame = self.tblVw_TargetSetting.frame;

self.tblVw_TargetSetting.frame = CGRectMake(0.0f, 223.0f,frame.size.width, frame.size.height);

_tblVw_TargetSetting.contentInset =  UIEdgeInsetsMake(0, 0, 0, 0);

[UIView commitAnimations];

}

5 个答案:

答案 0 :(得分:0)

根据您的要求,首先您需要像这样创建自定义UITableViewCell。

class PersonalInfoCell: UITableViewCell {

    @IBOutlet var txtField: UITextField!
    @IBOutlet var lblTitle: UILabel!

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

然后像这样实施UITableView委托方法

class PersonalInfoVC : UIViewController,UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate
{

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCell(withIdentifier: "PersonalInfoCell", for: indexPath as IndexPath) as! PersonalInfoCell
        cell.txtField.delegate = self
        cell.txtField.tag = indexPath.row
        return cell

    }


    func textFieldShouldReturn(textField: UITextField) -> Bool {
        return true
    }
}

我希望在上面这个简单的例子中你会有所了解。

答案 1 :(得分:0)

首先是您的自定义单元格类

class MyCell: UITableViewCell, UITextFieldDelegate{
    @IBOutlet var txtField: UITextField!
    @IBOutlet var lblTitle: UILabel!

    func configure() {
        self.txtField.delegate = self
        //This can also be done through storyboard(xib)
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        //Do your work
        return true
    }

    func textFieldDidBeginEditing(_ textField: UITextField) {
        //Do your work
    }
}

只需从cellForRow调用configure方法,就可以直接调用委托方法。

答案 2 :(得分:0)

它对我有用。请试试。

请将cellForRowAtIndexPath方法设置为:

textField.delegate = self;
textField.tag = indexPath.row;

然后在didbeginedit方法中写。

UITextField *txtfld1 = (UITextField *)[yourTableName viewWithTag:0]; 
UITextField *txtfld1 = (UITextField *)[yourTableName viewWithTag:1];

如果您没有文本字段,则可以使用for循环。

我希望对你有所帮助。谢谢。

答案 3 :(得分:0)

确保您已为您的班级实施了UITextFieldDelegate。在创建单元格时,首先为每个textField设置标记。

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "YourCellIdentifier", for: indexPath as IndexPath) as! YourCell
    cell.txtField.delegate = self
    cell.txtField.tag = indexPath.row
    return cell
}

现在,只要调用textField委托方法,就可以根据标记

执行操作
func textFieldDidBeginEditing(textField: UITextField!) {    
    if textField.tag == 1 {
        // perform actions here
    }
} 

func textFieldShouldReturn(textField: UITextField!) -> Bool {
    textField.resignFirstResponder()
    if textField.tag == 1 {
        //perform actions here
    }
    return true
}

答案 4 :(得分:0)

尝试以下步骤......

  1. 在cellForRowAtIndexPath方法中添加以下行

    cell.txtField?.addTarget(self,action:#selector(self.updateUserInput),for:UIControlEvents.editingChanged) cell.txtField?.tag =(indexPath as NSIndexPath).row if(indexPath.row == lastIndex){    cell.txtField?.returnKeyType = UIReturnKeyDone } 其他    cell.txtField?.returnKeyType = UIReturnKeyNext

  2. 创建模型类以更新不同单元格中所有文本字段的用户输入

  3. 在&lt; updateUserInput&#39;更新文本域中的文本到您的模型类方法

  4. 你的textFieldShouldReturn方法应该是这样的

    //Mark - UITextField delegates
        func textFieldShouldReturn(_ textField : UITextField) -> Bool {
            let returnTypeValue = textField.returnKeyType
            if returnTypeValue == .done || returnTypeValue == .default {
                textField.resignFirstResponder()
            }
            else if returnTypeValue == .next {
                let currentRow : Int = textField.tag
                if (currentRow + 1 < inputKeysArr.count) {
                    self.respondToField(currentRow + 1, errorField: "")
                }
                else {
                    textField.resignFirstResponder()
                }
            }
            return true
        }
    
        func respondToField(_ count: Int, errorField : String) {
        if count < inputKeysArr.count {
            //WE have more inputs to be made as become first responder
    
            let nextIndexPath : IndexPath = IndexPath.init(row: count, section: 0)
            tableView?.scrollToRow(at: nextIndexPath, at: UITableViewScrollPosition.middle, animated: true)
            scrollingCompletionClosure = {
                (weakRef: ViewController) -> Void in
            }
    
            let nonOptionalCell: UITableViewCell = UITableViewCell()
            let cell: UITableViewCell = (tableView!.cellForRow(at: nextIndexPath)) ?? nonOptionalCell
            if (cell.isKind(of: TableCell.self)) {
                let cell : TableCell = cell as! TableCell
                cell.txtField?.becomeFirstResponder()
            }
        }
    }
    
  5. 希望这会有所帮助......