UITextField in UITableViewCell Help

后端 未结 4 1356
半阙折子戏
半阙折子戏 2020-12-03 08:36

I have scoured the internet looking for a good tutorial or posting about having a UITableView populated with a UITextField in each cell for data entry.

I want to kee

4条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-03 09:07

    I have used Textfields in tableview for data entry. I have customised the UITextField class in a separate class called Utility :

    In Utility.h

     @interface CustomUITextField:UITextField{
            NSInteger rowNumber;
        }
    

    In Utility.m

    @implementation CustomUITextField
    
    @synthesize rowNumber;
    @end
    

    My tableView cellForRowAtIndexPath method is

    - (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        static NSString *Identifier = @"Cell";
        UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:Identifier];
        if(cell == nil)
            cell = [self reuseTableViewCellWithIdentifier:Identifier withIndexPath:indexPath];
    
        CustomUITextField *itemNameTextField = (CustomUITextField *)[cell.contentView viewWithTag:TEXTFIELD_TAG];//this is the tag I have set in reuseTableViewCellWithIdentifier method for textfield
        itemNameTextField.rowNumber = indexPath.row;
        itemNameTextField.text = @"";//you can set it for the value you want
        if(itemListTable.editing)
            itemNameTextField.borderStyle = UITextBorderStyleRoundedRect;
        else
            itemNameTextField.borderStyle = UITextBorderStyleNone;
    
    
    
        return cell;
    
    }
    

    You can customise the delegate methods of UITextField for CustomUITextField & can save the text entered in a particular row's textfield by accessing the CustomTextField's row number.

    Just try with this.

提交回复
热议问题