0

我有UITextField一个UITableView标题。当用户在 中输入任何内容时UITextFields,该条目将添加到NSMutableArray.

我想在每次调用时使用条目UITableCell的值创建一个新的。UITextFieldtextFieldShouldReturn

我尝试使用reloadData,但这似乎完全清空了我的数组。

有什么建议么?谢谢

 - (BOOL)textFieldShouldReturn:(UITextField *)task
{

[task resignFirstResponder];   
[self.tasksArray addObject:task.text];

NSLog(@"Test %@", self.tasksArray);
}

我的 cellForRowAtIndexPath

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

static NSString *CellIdentifier = @"Cell";
SwipeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (!cell) {
    cell = [[SwipeTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                     reuseIdentifier:CellIdentifier];
}

[cell setDelegate:self];
[cell.contentView setBackgroundColor:[UIColor whiteColor]];

// Setting the default inactive state color to the tableView background color
[cell setDefaultColor:self.tableView.backgroundView.backgroundColor];
[cell setSelectionStyle:UITableViewCellSelectionStyleGray];
[cell.textLabel setText:self.task.text];
self.view = tableView;

cell.mode = SwipeTableViewCellModeExit;
return cell;
}

我的UITableView标题:

- (UIView *)tableView:(UITableView *)tableView 
            viewForHeaderInSection:(NSInteger)section{

float width = tableView.bounds.size.width;
int fontSize = 18;
int padding = 1;

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0,width, fontSize+11)];
view.backgroundColor = [UIColor colorWithWhite:0 alpha:0];
view.userInteractionEnabled = YES;
view.tag = section;

UITextField *task = [[UITextField alloc] initWithFrame:CGRectMake(padding, 2, 
                                           width - padding , fontSize + 11)];
task.text = @"Type here";
task.borderStyle = UITextBorderStyleRoundedRect;
task.textAlignment = NSTextAlignmentCenter;
task.backgroundColor = [UIColor whiteColor];
task.textColor = [UIColor blackColor];
task.delegate = self;
task.font = [UIFont boldSystemFontOfSize:fontSize];

[view addSubview:task];
self.tasksArray = [[NSMutableArray alloc] init];

return view;
}

viewDidLoad

- (void)viewDidLoad {

[super viewDidLoad];
self.tasksArray = [NSMutableArray arrayWithCapacity:0];
self.title = @"Test";
self.navigationController.navigationBar.tintColor = [UIColor darkGrayColor];

UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[backgroundView setBackgroundColor:[UIColor colorWithRed:227.0 / 255.0 
                                                   green:227.0 / 255.0 
                                                    blue:227.0 / 255.0 
                                                   alpha:1.0]];
[self.tableView setBackgroundView:backgroundView];

}
4

2 回答 2

0

此行错误地提取数据:[cell.textLabel setText:self.task.text]; self.view = tableView;

它应该来自您的阵列。

你也永远不会重新加载数据,那应该是 textFieldShouldReturn 的最后一行

于 2013-10-12T16:38:25.533 回答
0

消除 [cell.textLabel setText:self.task.text];

进而,

[self.yourTableOutletName reloadData];之后插入[self.tasksArray addObject:task.text];

现在,您的代码将将对象添加到self.taskArray.

进而,

在 tablView 中反映您的数组,请使用此行。

[cell.textLabel setText:[self.tasksArray objectAtIndex:indexPath.row]];之后插入[cell setSelectionStyle:UITableViewCellSelectionStyleGray];

于 2013-10-12T16:55:41.617 回答