Objective-C、iOS SDK、NSMutableArray、添加元素并保存数据

发布于 2025-01-08 15:24:59 字数 1802 浏览 3 评论 0原文

基本上,每次我在文本字段中输入内容并将其保存到表中时,前一个条目都会被新条目替换。如何在可变数组中添加元素并保存最后一个条目?我

[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;
}

Basically, every time I enter something in my textfield and save it to the table, the previous entry will be replaced by the new one. How do I add elements in a mutable array and save the last entry? I tried

[tabledata addObject....

and in the entry

tabledata.lastObject objectAtIndex...

but that didn't work.

here is what I have:

 -(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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜爱皱眉﹌ 2025-01-15 15:24:59

在没有看到更多代码的情况下,我能做的最好的就是建议这样的内容

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

这假设 newTitlenewSubtitle 都是您想要添加的 NSString tableView 是指向 UITableView 的指针。

Without seeing more code, the best I can do is suggest something like this

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

This assumes both newTitle and newSubtitle are the NSStrings you wish to add and that tableView is the pointer to the UITableView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文