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

当我尝试从所有这些uitextfields中获取数据时,我只为可见的对象获取数据,而对于其他用户则为null。我不应该使用重用标识符。

以下是我的CellfoRrowAtIndex代码:

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的任何文本字段。

最佳答案

您可以在自定义单元中实现协议,并将所有更改传递给表视图并手动进行处理:

// 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来更新值。

关于iphone - 使用uitextfields创建uitableview表单,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17647941/

10-08 23:30