UITextfield 值已成功更新,但未在 table 中显示

UITextfield value successfully updated, but not shown in table

在我的应用程序中,行从不同的视图添加到我的 TableView。当用户添加行时,用户将返回到 TableView。问题是以前输入的文本不再显示。

我可以用 NSMutableDictionary 加载它,但用户看不到它。关于我应该做什么的任何想法?我应该添加什么代码以及我应该在哪里添加它?非常感谢!

这是来自 tableview 方法的代码。我认为修复会在这里的某个地方进行。

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

if (cell == nil) {
    cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    cell.wtf = [[UITextField alloc]init];

    NSUInteger count =0;
    for (NSMutableDictionary *search in dataForAllRows){ //this just helps me pull the right data out of an array of NSMutableDictionary's

        if ([search valueForKey:@"indexSection"] == [NSNumber numberWithInteger:(indexPath.section -1)]) {

            if ([search valueForKey:@"indexRow"] == [NSNumber numberWithInteger:indexPath.row]) {

                NSMutableDictionary *match = [dataForAllRows objectAtIndex:count];
                [cell.wtf setText:[match objectForKey:@"wtf"]]; 
                NSLog(@"%@",cell.wtf.text); // this outputs the correct value in the command line
            }
        }
        count++;
     }
   }
}

这是我的 CustomCell.m

的代码
#import "CustomCell.h"

@implementation CustomCell
@synthesize wtf, cellPath;

- (void)awakeFromNib {
// Initialization code

 } 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(void)layoutSubviews{
wtf = [[UITextField alloc] initWithFrame:CGRectMake(7, 3, 65, self.contentView.bounds.size.height-6)];

self.wtf.delegate = self;

[wtf setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[wtf setAutocorrectionType:UITextAutocorrectionTypeNo];
[wtf setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[wtf setBorderStyle:UITextBorderStyleRoundedRect];
wtf.textAlignment = NSTextAlignmentCenter;
wtf.keyboardType = UIKeyboardTypeNumberPad;  //
[wtf setAutocapitalizationType:UITextAutocapitalizationTypeWords];
[wtf setPlaceholder:@"enter"];

[self.contentView addSubview:wtf];
}

看起来您只是在设置文本字段的文本值(如果该单元格不存在)并将您的文本字段实例覆盖为没有@danh 提到的框架的实例。我相信您想做的是在将文本字段添加到您的单元格 contentview 后重用该文本字段,并更改该文本字段为每个索引路径显示的内容。

尝试重构您的单元格代码,使其更像:

@implementation ExerciseCell

#pragma mark - Init

- (id)initWithStyle:(UITableViewCellStyle)style
    reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style
                reuseIdentifier:reuseIdentifier];

    if (self)
    {
        wtf = [[UITextField alloc] initWithFrame:CGRectMake(7, 3, 65, 44)];

        wtf.delegate = self;

        [wtf setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
        [wtf setAutocorrectionType:UITextAutocorrectionTypeNo];
        [wtf setAutocapitalizationType:UITextAutocapitalizationTypeNone];
        [wtf setBorderStyle:UITextBorderStyleRoundedRect];
        wtf.textAlignment = NSTextAlignmentCenter;
        wtf.keyboardType = UIKeyboardTypeNumberPad; 
        [wtf setAutocapitalizationType:UITextAutocapitalizationTypeWords];
        [wtf setPlaceholder:@"enter"];

        [self.contentView addSubview:wtf];        
    }

    return self;
}

你的 tableview 数据源 class 更像

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

   static NSString *CellIdentifier = @"Cell";
   CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

   if (cell == nil) {
       cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

       [cell.wtf setDelegate:self];
   }

    NSUInteger count = 0;
    for (NSMutableDictionary *search in dataForAllRows){ //this just helps me pull the right data out of an array of NSMutableDictionary's

        if ([search valueForKey:@"indexSection"] == [NSNumber numberWithInteger:(indexPath.section -1)]) {

            if ([search valueForKey:@"indexRow"] == [NSNumber numberWithInteger:indexPath.row]) {

                NSMutableDictionary *match = [dataForAllRows objectAtIndex:count];
                [cell.wtf setText:[match objectForKey:@"wtf"]]; 
                NSLog(@"%@",cell.wtf.text); // this outputs the correct value in the command line
            }
        }
        count++;
     }
   }
}

您的意思是还要将 textField 的委托赋值两次?一次在单元格中,一次在表视图的数据源中?

考虑在 IB 中将标识符为@"Cell" 的单元格定义为 table 的原型行。然后,使用 dequeueReusableCellWithIdentifier:forIndexPath: 检索 cellForRowAtIndexPath 中的单元格。更容易理解单元格的外观,并且可以避免在代码中定义子视图时常见的一些错误。

说到常见错误,您的代码似乎出现了两个错误:它既没有构建文本字段,也没有将其添加为单元格的子视图。两者都会解释没有看到文本字段。

@williamb 的建议是正确且必要的:如果单元格的子视图不存在,则只构建它们,但单元格的构建是不完整的...

if (cell == nil) {
    cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    UITextField *wtf = [[UITextField alloc]initWithFrame:CGRectMake(10,10,200,42];
    [wtf setDelegate:self];
    [cell addSubview:wtf];
    cell.wtf = wtf;
}

正如我在评论中提到的,分段 table 应该由二维数组支持。外部数组是一个节数组。每个部分数组都是一个字典数组,等于您每次通过此方法搜索的字典,但是是预先安排的,因此在 cellForRowAtIndexPath 中完成的所有操作都是索引到一个数组中:

NSDictionary *d = self.myCorrectlyStructuredModel[indexPath.section][indexPath.row];
cell.wtf.text = d[@"wtf"];

用现有的东西构建它并不是什么大挑战。考虑在解决文本字段问题后立即执行此操作。我(或其他人)可以给你一些关于如何构建该结构的建议——如果你需要的话。

为了在CustomCell中将文本加载到UITextField中,我添加了以下方法

CustomCell.m

-(void)viewMyCellData{
//here I can set text to my textfield
wtf.text = @"Desired Text"; //this will read in every wtf textfield in the table
//getting the right text from an array will be asked in another question that I will post
//in a comment to this answer
}

接下来我们使用 [self viewMyCellData] 调用它 在我们的最后 -(void)layoutSubviews 方法也在 CustomCell.m