【问题标题】:Creating uitableview forms with uitextfields使用 uitextfields 创建 uitableview 表单
【发布时间】:2013-07-15 06:25:56
【问题描述】:

我正在研究一个我有一个 uitableview 的地方,我想在它的每个单元格中添加一个 uitextfield,以便它可以用作表单。大约有 15 个这样的 uitextfields。但是由于我在屏幕上的空间很小,一次只显示 5 行,而其他行则出现在滚动中。

当我尝试从所有这些 uitextfields 中获取数据时,我只会为那些可见的人获取数据,而对于其他人,我会得到空值。应该是因为我没有使用重用标识符。

以下是我的 CellforRrowAtIndex 代码:

editContactCell *tcell = (editContactCell*) [tableView dequeueReusableCellWithIdentifier:nil];
    NSArray *nib= [[NSBundle mainBundle] loadNibNamed:@"editContactCell" owner:self options:nil];
    tcell = (editContactCell *)[nib objectAtIndex:0];
    tcell.accessoryType = UITableViewCellAccessoryNone;
    tcell.selectionStyle = UITableViewCellSelectionStyleNone;

    tcell.txtDetails.tag = indexPath.row+50;
    if([dictClientDetails count]>0){
        if(indexPath.section==0 && indexPath.row==0){
            tcell.lblTitle.text = @"Mobile:";
            tcell.txtDetails.placeholder = @"Mobile:";
            if([dictClientDetails count]>0){
                tcell.txtDetails.text = [dictClientDetails objectForKey:@"Mobile"];
            }
        }
        else if(indexPath.section==0 && indexPath.row==1){
            tcell.lblTitle.text = @"Work:";
            tcell.txtDetails.placeholder = @"Work:";
            if([dictClientDetails count]>0){
                tcell.txtDetails.text = [dictClientDetails objectForKey:@"Work_number"];
            }
        }
        else if(indexPath.section==1 && indexPath.row==0){
            tcell.lblTitle.text = @"Email:";
            tcell.txtDetails.placeholder = @"Email:";
            if([dictClientDetails count]>0){
                tcell.txtDetails.text = [dictClientDetails objectForKey:@"Email"];
            }
        }
        else if(indexPath.section==2 && indexPath.row==0){
            tcell.lblTitle.text = @"IM1:";
            tcell.txtDetails.placeholder = @"IM1:";
            if([dictClientDetails count]>0){
                tcell.txtDetails.text = [dictClientDetails objectForKey:@"IM1"];
            }
        }
        else if(indexPath.section==2 && indexPath.row==1){
            tcell.lblTitle.text = @"IM2:";
            tcell.txtDetails.placeholder = @"IM2:";
            if([dictClientDetails count]>0){
                tcell.txtDetails.text = [dictClientDetails objectForKey:@"IM2"];
            }
        }
        else if(indexPath.section==3 && indexPath.row==0){
            tcell.lblTitle.text = @"Based In:";
            tcell.txtDetails.placeholder = @"Based In:";
            if([dictClientDetails count]>0){
                tcell.txtDetails.text = [dictClientDetails objectForKey:@"Location"];
            }
        }
        else if(indexPath.section==3 && indexPath.row==1){
            tcell.lblTitle.text = @"Address:";
            tcell.txtDetails.placeholder = @"Address:";
            if([dictClientDetails count]>0){
                tcell.txtDetails.text = [dictClientDetails objectForKey:@"Address"];
            }
        }
        else if(indexPath.section==4 && indexPath.row==0){
            tcell.lblTitle.text = @"Division:";
            tcell.txtDetails.placeholder = @"Division:";
            if([dictClientDetails count]>0){
                tcell.txtDetails.text = [dictClientDetails objectForKey:@"Division"];
            }
        }
        else if(indexPath.section==4 && indexPath.row==1){
            tcell.lblTitle.text = @"Department:";
            tcell.txtDetails.placeholder = @"Department:";
            if([dictClientDetails count]>0){
                tcell.txtDetails.text = [dictClientDetails objectForKey:@"Department"];
            }
        }
    }
    return tcell;

我正在尝试获取以下数据:

NSLog(@"1 %@",((editContactCell*)[tblvwEditContactDetails cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]]).txtDetails.text);
NSLog(@"2 %@",((editContactCell*)[tblvwEditContactDetails cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]]).txtDetails.text);
NSLog(@"3 %@",((editContactCell*)[tblvwEditContactDetails cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]]).txtDetails.text);
NSLog(@"4 %@",((editContactCell*)[tblvwEditContactDetails cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:2]]).txtDetails.text);
NSLog(@"5 %@",((editContactCell*)[tblvwEditContactDetails cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:2]]).txtDetails.text);
NSLog(@"6 %@",((editContactCell*)[tblvwEditContactDetails cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:3]]).txtDetails.text);
NSLog(@"7 %@",((editContactCell*)[tblvwEditContactDetails cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:3]]).txtDetails.text);
NSLog(@"8 %@",((editContactCell*)[tblvwEditContactDetails cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:4]]).txtDetails.text);
NSLog(@"9 %@",((editContactCell*)[tblvwEditContactDetails cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:4]]).txtDetails.text);

但只有可见的行有任何文本字段,其他人返回 null。

【问题讨论】:

    标签: iphone ios ipad uitableview


    【解决方案1】:

    您可以在您的自定义单元格中实现一个协议并将每个更改传递给您的 tableview 并手动处理:

    // Cell.h
    @protocol UpdateValueProtocol <NSObject>
    
    -(void)updateValue:(id)value forKey:(NSString *)key;
    
    @end
    
    @interface Cell : UITableViewCell <UITextFieldDelegate>
    ...
    
    @property (nonatomic, assign) UITableViewController<UpdateValueProtocol> *parent;
    @end
    
    // Cell.m
    -(BOOL)textFieldShouldReturn:(UITextField *)textField
    {
        [parent updateValue:textField.text forKey:lblTitle.text];
        return YES;
    }
    
    // ParentTableView.h
    @interface ParentTableView<UpdateValueProtocol>
    {
        NSMutableDictionary *contactDictionary;
    }  
    
    // ParentTableView.m
    -(void)updateValue:(id)value forKey:(NSString *)key
    {
        if(!contactDictionary)
            contactDictionary = [NSMutableDictionary dictionary];
        [contactDictionary setObject:value forKey:key];
    }
    

    不知道你的dictClientDetails 是干什么用的,但你也可以用它代替contactDictionary 来更新值。

    【讨论】:

      【解决方案2】:

      您还应该查看数据输入框架,例如免费的 Sensible TableView 框架。该框架将自动从您的数据对象生成所有单元格,并将处理从单元格读取/写入的所有数据。在我的项目中节省大量时间。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        • 2021-07-11
        • 2018-04-22
        • 1970-01-01
        • 1970-01-01
        • 2011-04-30
        相关资源
        最近更新 更多