0

我创建了一个带有 a 的表视图控制器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];

我知道这是一个常见的问题,但我一直在讨价还价。

4

2 回答 2

0

如果你不初始化chatHistory它甚至不存在......没关系是空的!(我不清楚这里的“私人会员”是什么意思。)

如果您记录 的值chatHistory,您将确定它是 nil 还是空。

我看到的另一个问题是您尝试chatHistory使用其他 ( history) 数组加载,然后将其内容用作字符串。您应该决定它是数组数组还是字符串数组,然后保持一致。

[[self.chatHistory alloc] init];也不是创建新对象的有效语法。请查看Objective-C 语言参考以了解正确的方法。)

于 2012-11-21T13:05:53.930 回答
0

我想你忘了分配和初始化你的chatHistory

- (void)viewDidLoad
{
    self.chatHistory = [[NSMutableArray alloc] init];
}
于 2012-11-21T13:03:07.307 回答