2011-11-02 35 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

相關問題