有条件地将单元格数据添加到NSMutableArray

时间:2014-02-18 22:39:39

标签: ios iphone objective-c uitableview

今天遇到很多麻烦。最后决定转向专家。

我有一个标准的表视图控制器。每个单元格都有一个内置的UIImage,带有一个轻敲手势识别器,可以将图像从绿色复选标记切换到红色x。这工作得很好。

我现在要做的是写一个条件语句,说明:当图像是绿色复选标记时,将单元格textLabel添加到saved NSMutableArray;否则从数组中删除红色x对象(可能是在较早时间添加的)。目前我在两个帐户的控制台中都获得了“null”。

这是原始方法:

-(void)imageTapped:(UIGestureRecognizer*)gesture
{

    UIImageView *selectedImageView=(UIImageView*)[gesture view];
    UIImage *imageRed = [UIImage imageNamed:@"checkmark(red).png"];
    UIImage *imageGreen = [UIImage imageNamed:@"checkmark(green).png"];

    NSString *address = @" ";
    NSString *name = @" ";
    UITableViewCell *cell = [self.placesTableView cellForRowAtIndexPath:self.placesTableView.indexPathForSelectedRow];
    name = cell.textLabel.text;
    address = cell.detailTextLabel.text;
    NSMutableArray *saved = [[NSMutableArray alloc] initWithObjects:@"%@",@"%@", name, address, nil];
    NSString *csv = [NSString stringWithFormat:@"%@, %@", name, address];

    if (selectedImageView.image == imageGreen) {
        selectedImageView.image = imageRed;
        [saved removeObject:csv];
        NSLog(@"Deselected" @"%@", name);
    } else {
        selectedImageView.image = imageGreen;
        [saved addObject:csv];
        NSLog(@"Selected" @"%@", name);
    }
}

编辑:建议的方法(仍然无效):

h:@property (strong, nonatomic) NSMutableArray *saved;

米:

- (void)viewDidLoad
{
    _saved = [[NSMutableArray alloc] init];
}

    -(void)imageTapped:(UIGestureRecognizer*)gesture
{

    UIImageView *selectedImageView=(UIImageView*)[gesture view];
    UIImage *imageRed = [UIImage imageNamed:@"checkmark(red).png"];
    UIImage *imageGreen = [UIImage imageNamed:@"checkmark(green).png"];
    NSString *address = @" ";
    NSString *name = @" ";
    UITableViewCell *cell = [self.placesTableView  cellForRowAtIndexPath:self.placesTableView.indexPathForSelectedRow];
    name = cell.textLabel.text;
    address = cell.detailTextLabel.text;
    NSString *csv = [NSString stringWithFormat:@"%@, %@", name, address];
    self.saved = [NSMutableArray array];

    //NSData *imageRedData = UIImagePNGRepresentation(imageRed);
    NSData *imageGreenData = UIImagePNGRepresentation(imageGreen);
    NSData *selectedImageViewData = UIImagePNGRepresentation(selectedImageView.image);

    if ( [selectedImageViewData isEqual: imageGreenData]) {
        selectedImageView.image = imageRed;
        [self.saved removeObject:csv];
        NSLog(@"Deselected" @"%@", name);
    } else {
        selectedImageView.image = imageGreen;
        [self.saved addObject:csv];
        NSLog(@"Selected" @"%@", name);
    }
}

2 个答案:

答案 0 :(得分:1)

你的意思是每次都创建保存的可变数组吗?它不会持续超过imageTapped方法的结尾。也许你想要这个NSMutableArray是一个属性(或ivar)并添加或从self.saved中删除?

答案 1 :(得分:0)

你需要更改UIImage比较逻辑,而不是在NSObject上使用=运算符使用isEqual方法。这是一个可以帮助您的解决方案代码。

 -(void)imageTapped:(UIGestureRecognizer*)gesture
 {

     UIImageView *selectedImageView=(UIImageView*)[gesture view];
     UIImage *imageRed = [UIImage imageNamed:@"checkmark(red).png"];
     UIImage *imageGreen = [UIImage imageNamed:@"checkmark(green).png"];
     NSString *address = @" ";
     NSString *name = @" ";
     UITableViewCell *cell = [self.placesTableView  cellForRowAtIndexPath:self.placesTableView.indexPathForSelectedRow];
     name = cell.textLabel.text;
     address = cell.detailTextLabel.text;
     NSMutableArray *saved = [[NSMutableArray alloc] initWithObjects:@"%@",@"%@", name, address, nil];
     NSString *csv = [NSString stringWithFormat:@"%@, %@", name, address];

    NSData *imageRedData = UIImagePNGRepresentation(imageRed);
    NSData *imageGreenData = UIImagePNGRepresentation(imageGreen);
    NSData *selectedImageViewData = UIImagePNGRepresentation(selectedImageView.image);  

    if ( [selectedImageViewData isEqual: imageGreenData]) {
         selectedImageView.image = imageRed;
        [saved removeObject:csv];
        NSLog(@"Deselected" @"%@", name);
    } else {
        selectedImageView.image = imageGreen;
        [saved addObject:csv];
        NSLog(@"Selected" @"%@", name);
  }
}