NSString字段随机更改其类型

时间:2014-09-09 10:08:19

标签: ios uitableview nsstring

我有一个让我疯狂的问题。

情况 :我每次都会使用新的TableViewCells填充表格。有3个单元格,每个单元格都有一个UILabel和UITextField。对于表重绘(当由于滚动单元格离开屏幕并且用户向后滚动以查看此单元格时),会创建TableViewCell,UILabel和UITextField的新对象。 我想动态地将TextField的输入值保存到NSString并创建这样的NSString字段,并在TextField监听器UIControlEventEditingChanged中用值填充它。

问题 :当滚动回单元格必须插入表格后,我想用Text填充TextField,保存在NSString字段中。 BUT!每次此字段变量更改其类型。有我的日志 - 你可以看到第一个表创建,当我的NSString有适当的类型NSCFString。您还可以在其中看到TextField的UIControlEventEditingChanged-listener和使用>>的日志。 scrool后我想要的信号将NSString-field值插入新单元格中的TextFied。在设置文本之前,我检查我的“NSString”字段的类型,你可以看到每次我有这个字段的随机类型。 有人能用这段代码解释我的WTF吗?我是Iphone的新手,所以我猜问题在于客观原则c

    // first table creation - all right
2014-09-09 11:34:31.713 BooksPager[1436:207] lazyPatient Class >> NSCFString
2014-09-09 11:34:31.714 BooksPager[1436:207] lazyPatient Value >> 

==========================
// typing text into TextField
2014-09-09 11:37:05.371 BooksPager[1465:207] lazyPatientNameSelected = q
2014-09-09 11:37:05.561 BooksPager[1465:207] lazyPatientNameSelected = qw
2014-09-09 11:37:05.769 BooksPager[1465:207] lazyPatientNameSelected = qwe
// *scrolling and cell goes out of screen
// *scrolling back
// *new cell created with new TextField object, get type of lazyPatientNameSelected variable 
//  before set it value to TextField
2014-09-09 11:36:13.215 BooksPager[1436:207] lazyPatient Class >> __NSArrayM  //HERE!
2014-09-09 11:36:14.416 BooksPager[1436:207] lazyPatient Value >> (
    "<WebThreadCaller: 0x4c60f40>",
    "<WebThreadCaller: 0x4c60f40>"
)

2014-09-09 11:37:05.371 BooksPager[1465:207] lazyPatientNameSelected = a
2014-09-09 11:37:05.561 BooksPager[1465:207] lazyPatientNameSelected = as
2014-09-09 11:37:05.769 BooksPager[1465:207] lazyPatientNameSelected = asf
2014-09-09 11:37:14.989 BooksPager[1465:207] lazyPatient Class >> __NSCFSet  //HERE!
2014-09-09 11:37:16.164 BooksPager[1465:207] lazyPatient Value >> {(
)}

2014-09-09 11:47:02.392 BooksPager[1541:207] lazyPatientNameSelected = a
2014-09-09 11:47:02.532 BooksPager[1541:207] lazyPatientNameSelected = as
2014-09-09 11:47:11.205 BooksPager[1541:207] lazyPatient Class >> NSInvocation  //HERE!
2014-09-09 11:47:12.243 BooksPager[1541:207] lazyPatient Value >> <NSInvocation: 0x4c57c70>

2014-09-09 12:49:25.502 BooksPager[1720:207] lazyPatientNameSelected = q
2014-09-09 12:49:25.631 BooksPager[1720:207] lazyPatientNameSelected = qw
2014-09-09 12:49:25.728 BooksPager[1720:207] lazyPatientNameSelected = qwe
2014-09-09 12:49:33.221 BooksPager[1720:207] lazyPatient Class >> UITextTapRecognizer  //HERE!
2014-09-09 12:49:39.450 BooksPager[1720:207] lazyPatient Value >> <UITextTapRecognizer: 0x4c98ee0; state = Possible;     delaysTouchesEnded = NO; view = <UITextField 0x4c84030>; target= <(action=oneFingerDoubleTap:,     target=<UITextInteractionAssistant 0x4c884b0>)>; numberOfTapsRequired = 2>

这是我的代码:

@interface PatientCardViewController : UITableViewController {

    NSString *lazyPatientNameSelected;

}

@end

@implementation PatientCardViewController

// .  .  .  .  

- (void)viewDidLoad
{
    [super viewDidLoad];   
    lazyPatientNameSelected = @"";
}

// .  .  .  .  

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";


switch ([indexPath section]) {
    case 0:
    {
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        cell.frame = CGRectMake(0, 0, 320, 130);
        cell.contentView.backgroundColor = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];


        UILabel *lIPServerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 30)];
        lIPServerLabel.text = @"Enter patient's last name";


        UITextField *tfServerPath = [[UITextField alloc] initWithFrame:CGRectMake(10, 50, 300, 30)];
        tfServerPath.adjustsFontSizeToFitWidth = YES;
        tfServerPath.textColor = [UIColor blackColor];

        tfServerPath.backgroundColor = [UIColor whiteColor];
        tfServerPath.tag = 11;

        NSLog(@"lazyPatient Class >> %@", NSStringFromClass([lazyPatientNameSelected class]));
        NSLog(@"lazyPatient Value >> %@", lazyPatientNameSelected);

        tfServerPath.text = lazyPatientNameSelected;

        [tfServerPath addTarget:self action:@selector(textFieldDidBeginEditing:) forControlEvents:UIControlEventEditingChanged];


        [cell.contentView addSubview:lIPServerLabel];
        [cell.contentView addSubview:tfServerPath];              

        [lIPServerLabel release];                
        [tfServerPath release];


        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        return cell;
    }   

        break;

    case 1:
    {
    // .  .  .  .  similar  .  .  .  .
        break;

    case 2:        
    // .  .  .  .  similar  .  .  .  .    
        break;
    }
}

- (void) textFieldDidBeginEditing :(UITextField*) theTextField{    

    switch (theTextField.tag) {
    case 11:

        lazyPatientNameSelected = theTextField.text;
        NSLog(@"lazyPatientNameSelected = %@", lazyPatientNameSelected);

        break;
    }

}

@end

重复使用细胞不合适。这是我真实代码的简化

2 个答案:

答案 0 :(得分:0)

似乎问题就在这一行 tfServerPath.text = lazyPatientNameSelected - 更改tfServerPath.text = [lazyPatientNameSelected copy];

答案 1 :(得分:0)

呀!我发现了问题所在。我使用了textFieldDidBeginEditing()方法来侦听输入到文本字段中的每个新符号。 但是当我开始使用textFieldDidEndEditing()方法时 - 问题消失了,我从lazyPatient变量得到了一个值。