Iphone 为什么不';我的NSMutableArray对象是否立即添加到UITableView?

Iphone 为什么不';我的NSMutableArray对象是否立即添加到UITableView?,iphone,objective-c,ios,xcode,nsmutablearray,Iphone,Objective C,Ios,Xcode,Nsmutablearray,我有一个NSMutableArray,其中包含对象,我使用以下代码在UITableView中添加/删除对象。它只能在关闭并重新启动应用程序后工作,而不是马上。为什么会这样?这是我的代码(maincelltext是单元格中的标题,subtitlecelltext是字幕): 编辑:这是我的UITableView代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *

我有一个NSMutableArray,其中包含对象,我使用以下代码在UITableView中添加/删除对象。它只能在关闭并重新启动应用程序后工作,而不是马上。为什么会这样?这是我的代码(maincelltext是单元格中的标题,subtitlecelltext是字幕):

编辑:这是我的UITableView代码:

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

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

cell.textLabel.font = [UIFont fontWithName:@"HelveticaBold" size:16.0];

cell.textLabel.adjustsFontSizeToFitWidth = YES; 
cell.textLabel.numberOfLines = 1;

// Configure the cell.
UIImage *cellImage = [UIImage imageNamed:@"warning.png"];
CGRect cropRect = CGRectMake(0.0, 0.0, 12.0, 12.0);
CGImageRef croppedImage = CGImageCreateWithImageInRect([cellImage CGImage], CGRectMake(0.0f,0.0f,cellImage.size.width,cellImage.size.height));
UIImageView *myImageView = [[UIImageView alloc] initWithFrame:cropRect];
[myImageView setImage:[UIImage imageWithCGImage:croppedImage]]; 
CGImageRelease(croppedImage);
[self.view addSubview:myImageView];

cell.imageView.image = cellImage;

NSString *subjectString = [maincelltext objectAtIndex: [indexPath row]];

cell.textLabel.text = subjectString;

NSString *subtitle = [subtitlecelltext objectAtIndex: [indexPath row]];

cell.detailTextLabel.text = subtitle;

if(maincelltext.count == 0){

    NSString *notabledata = [NSString stringWithFormat:@"No Dates Set"];
    [maincelltext addObject:notabledata];

}

return cell;
} 

谢谢

您需要告诉UITableView它需要插入行。首先确定新对象现在所在的数组中的索引,然后告诉UITableView按如下方式插入行:

NSIndexPath *indexPathOfNewObject=[NSIndexPath indexPathForRow: newObjectIndex section:0];
NSArray *indexPathArray=[NSArray arrayWithObject: indexPathOfNewObject];

[self.tableView beginUpdates];
// Note that UITableViewRowAnimationAutomatic is for iOS5 only.
[self.tableView insertRowsAtIndexPaths: indexPathArray withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];
通过使用此方法,您将在tableView上获得凉爽的动画效果

或者,您可以只调用[self.tableView reloadData],它将只重新加载所有数据


祝你好运

只需执行
[myTableView重载数据]在执行
[myArray addObject:myObject]之后你应该没事。

你可以发布更多详细信息吗。这个问题像这样无法使用这还不足以理解您的问题,请发布更多代码。此外,通过这种方式,您不是从表中添加/删除元素,而是从数组中添加/删除元素;在数组中输入新对象后?请参阅下面的答案和我的答案…在将对象添加到数组中之后。这样你手头就有了索引,上面写着:file://localhost/Users/ReddexDesign/PixelBit%20Apps/School%20VLE%20Business/School%20VLE%20Business/ViewController.m: 错误:语义问题:使用未声明的标识符“newObjectIndex”newObjectIndex是已添加到数组中的对象的新索引。这是你必须弄清楚并提供的东西。这只是示例代码。不幸的是,仍然没有发生任何事情:'(
NSIndexPath *indexPathOfNewObject=[NSIndexPath indexPathForRow: newObjectIndex section:0];
NSArray *indexPathArray=[NSArray arrayWithObject: indexPathOfNewObject];

[self.tableView beginUpdates];
// Note that UITableViewRowAnimationAutomatic is for iOS5 only.
[self.tableView insertRowsAtIndexPaths: indexPathArray withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView endUpdates];