【发布时间】:2012-11-21 12:55:26
【问题描述】:
我创建了一个带有UITextField 的表视图控制器。如何填充来自UITextField 的输入文本的表格视图控制器单元格。我尝试了以下但似乎数组为空或被清空
- (IBAction)endingInput:(id)sender{
[sender resignFirstResponder];
//send the message to the table cell
NSMutableArray *history = [[NSMutableArray alloc] init];
[history addObject:[NSString stringWithFormat:@"%@",self.chatMessageField.text]];
[self.chatHistory addObject:history];
[myTableView reloadData];
//push the message to server
[self initiateMessageSending:self.chatMessageField.text];
chatMessageField.text = @"";
[history release];
}
关于我的cellForRowAtIndex 方法;
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if(tableView == self.myTableView){
cell.textLabel.text = [chatHistory objectAtIndex:indexPath.row];
}
return cell;
我已经设置了一个断点,但它没有通过那里。我怀疑我的数组是空的。
同样,调用reloadData 方法的最佳位置在哪里?
另外,chatHistory 是私有成员,有没有办法像[[self.chatHistory alloc] init]; 一样初始化它?
我知道这是一个常见问题,但我一直在讨价还价。
【问题讨论】:
-
你初始化 chatHistory 了吗?
-
@PratyushaTerli - 现在我做到了,但仍然无济于事,我有 NSLog 我的数组,它显示它有我制作的编码文本输入,我不能做的是让内容要显示到 tableview 单元格的数组
-
注释掉
if(tableView == self.myTableView){和}并尝试 -
我认为这里的每个人的答案都添加到了解决问题的检查中,但有趣的是,罪魁祸首是表没有委托和数据源连接......所以总是在检查连接之前做任何其他事情。对于给您带来的麻烦,我深表歉意,并感谢您的及时回复。
标签: ios arrays controller nsmutablearray tableview