iphone - 当选择行时如何将单元格的标题保存到 NSMutableArray 中?

标签 iphone ios uitableview nsmutablearray

我有一个表格,需要将所选单元格的标题保存到数组中。我的 NSMutableArray 称为 dataArray,我正在使用这一行

        [categoryItemSelected addObject: dataArray];

保存所选项目。我希望将多个项目添加到数组中 - 用户应该能够根据需要选择尽可能多或尽可能少的行。每一个都会被添加到 dataArray 中。

这是我的表 didSelectAtIndexPath: 方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    int index = indexPath.row; id obj = [listOfCategories objectAtIndex:index];


    //This toggles the checkmark
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];


    if (cell.accessoryType == UITableViewCellAccessoryNone)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

        UIImage *image = [UIImage imageNamed:@"icon-tick.png"];

        UIButton *downloadButton = [UIButton buttonWithType:UIButtonTypeCustom];

        [downloadButton setImage:image forState:UIControlStateNormal];
        [downloadButton setFrame:CGRectMake(0, 0, 19, 19)];
        [downloadButton setBackgroundColor:[UIColor clearColor]];
        [tableView cellForRowAtIndexPath:indexPath].accessoryView = downloadButton;

        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        //This sets the array

    } else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;

        UIButton *downloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [downloadButton setTitle:@"" forState:UIControlStateNormal];
        [downloadButton setFrame:CGRectMake(0, 0, 0, 0)];
        [tableView cellForRowAtIndexPath:indexPath].accessoryView = downloadButton;

    }

    // Save text of the selected cell:
    UITableViewCell *cellSelected = [tableView cellForRowAtIndexPath:indexPath];

    if ([cellSelected.textLabel.text isEqualToString:@"Food & Drinks"]) {
        NSString *valueToSave = cellSelected.textLabel.text;
        [[NSUserDefaults standardUserDefaults]
         setObject:valueToSave forKey:@"preferenceName"];

    }

    NSString *valueToSave = cellSelected.textLabel.text;
    [[NSUserDefaults standardUserDefaults]
     setObject:valueToSave forKey:@"preferenceName"];


    NSString *savedValue = [[NSUserDefaults standardUserDefaults]
                            stringForKey:@"preferenceName"];

    NSLog(@"savedValue %@", savedValue);

    NSMutableData *data = [NSMutableData data];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    // Customize archiver here
    [archiver encodeObject:obj forKey:@"keyForYourArrayOfNSIndexPathObjects"];
    [archiver finishEncoding];
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"keyForYourArrayOfNSIndexPathObjects"];


    NSKeyedUnarchiver *unarchiver;
    unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:
                  [[NSUserDefaults standardUserDefaults] objectForKey:@"keyForYourArrayOfNSIndexPathObjects"]];
    // Customize unarchiver here
    categoryItemSelected = [unarchiver decodeObjectForKey:@"keyForYourArrayOfNSIndexPathObjects"];
    [unarchiver finishDecoding];
    if (categoryItemSelected == nil) {
        //If it isn't been created - then create new array

        [categoryItemSelected addObject: dataArray];

        NSLog(@"list of categories selected in dataArray %@", dataArray);

    }

    NSLog(@"list of categories selected in dataArray %@", dataArray);      

}

最佳答案

你把参数弄反了。要将此对象添加到 dataArray 中,请执行以下操作:

[dataArray addObject: categoryItemSelected];

您需要首先检查categoryItemSelected是否为nil,因为将nil对象插入到数组中会抛出异常。

关于iphone - 当选择行时如何将单元格的标题保存到 NSMutableArray 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14463229/

相关文章:

iphone - UILocalNotification 多次触发

ios - 快速将 UILabel 的内容包装在一个行单元格中

ios - 修改默认的 -Prefix.pch 文件

ios - 在 Xcode Auto Layout 中水平对齐两个相邻的对象

swift - 如何自定义 UIScrollview 的指示器位置?

ios - 在 UITableViewController 中使用自定义单元格时如何防止获取 nil 界面元素?

objective-c - 如何将 UIImage 从 iPhone 应用程序传递到 Apple Watch 应用程序

iphone - 如何以编程方式在 iOS 应用程序中显示目标的版本/内部版本号?

iphone - MP4 文件中的播放跳过/搜索

ios - dequeueReusableCellWithIdentifier 中的副标题