2012-02-23 49 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