2012-02-23 38 views
-1

基本上,每當我在我的文本字段中輸入內容並將其保存到表中時,以前的條目將被替換爲新的條目。如何在可變數組中添加元素並保存最後一項?我試圖Objective-C,iOS SDK,NSMutableArray,添加元素並保存數據

[tabledata addObject.... 

,並在進入

tabledata.lastObject objectAtIndex... 

,但沒有奏效。

這裏是我有:

-(void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    titlestring = [[NSUserDefaults standardUserDefaults] objectForKey:@"titlehomework" ]; 
    detailsstring = [[NSUserDefaults standardUserDefaults] objectForKey:@"detailshomework"]; 

    tabledata = [[NSMutableArray alloc] initWithObjects:titlestring, nil]; 

    tablesubtitles = [[NSMutableArray alloc] initWithObjects:detailsstring, nil]; 
} 

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; 
{ 
    return [tabledata count]; 
} 


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; 
{ 
    UITableViewCell *cell = nil; 

    cell = [tableView dequeueReusableCellWithIdentifier:@"homeworkcell"]; 

    if(cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"homeworkcell"]; 
    } 
    cell.textLabel.text = [tabledata objectAtIndex:indexPath.row]; 
    cell.detailTextLabel.text = [tablesubtitles objectAtIndex:indexPath.row]; 
    cell.textLabel.font = [UIFont systemFontOfSize:14.0]; 
    cell.textLabel.backgroundColor = [ UIColor clearColor ]; 
    cell.detailTextLabel.backgroundColor = [UIColor clearColor]; 

    //-----------------------------------------START----------------------------Set image of cell---- 
    cellImage = [UIImage imageNamed:@"checkboxblank.png"]; 
    cell.imageView.image = cellImage; 
    //--------------------------------------------END---------------------------end set image of cell-- 

    return cell; 
} 
+0

您需要在您試圖將對象添加到'tabledata'數組的位置發佈代碼。 – PengOne 2012-02-23 23:59:29

+0

*「保存最後一個條目」*中的含義*「如何在可變數組中添加元素並保存最後一個條目」*?你可以發佈代碼,你試圖做到這一點? – sch 2012-02-24 00:05:05

回答

1

沒有看到更多的代碼,我所能做的最好的建議是這樣的

[tabledata addObject:newTitle]; 
[tablesubtitles addObject:newSubtitle]; 
[tableView reloadData]; 

這假定兩者newTitlenewSubtitleNSString是你希望添加並且tableView是指向UITableView的指針。

+0

有道理,現在,我該如何在這裏調用它:'cell.textLabel.text = [tabledata objectAtIndex:indexPath.row];'我不能只使用tabledata – user1191343 2012-02-24 06:08:21