2011-11-02 100 views
-1

我有一个NSMutableArray内的对象,我使用下面的代码来添加/删除对象从UITableView。它的工作原理,但只有关闭和重新启动应用程序后,不要马上。为什么是这样?这里是我的代码(maincelltext是在细胞中的标题和subtitlecelltext是字幕):为什么我的NSMutableArray对象不能直接添加到UITableView中?

maincelltext = [[NSMutableArray alloc] init]; 
subtitlecelltext = [[NSMutableArray alloc] init]; 

[maincelltext addObject:@"TITLE"]; 
[subtitlecelltext addObject:@"SUBTITLE TEST"]; 

编辑:这是我的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; 
} 

谢谢!

+1

你可以请张贴一些更多的细节。此问题无法如此 –

+0

这不足以理解您的问题,发布更多代码。用这种方式更多的是,你不是从表中添加/删除元素,而是从数组中删除元素。 – Mat

+0

现在编辑为你:-) – pixelbitlabs

回答

1

你需要告诉UITableView它需要插入行。首先确定数组的新对象(S)现在是在索引,然后告诉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],它将重新加载所有数据。

祝你好运!

+0

我会在哪里放这段代码?谢谢你的回答! :-) – pixelbitlabs

+0

将对象添加到阵列后。这样你就可以获得索引了。 – timthetoolman

+0

添加代码以通知表视图正在进行更改。 – timthetoolman

1

只要做一个[myTableView reloadData];你做了[myArray addObject:myObject];后,你应该没问题。

+0

不幸的是,仍然没有任何反应:'( – pixelbitlabs

相关问题