2

I am trying to select multiple UITableViewCells, it is working fine, but I can't add strings to my NSMutableArray.

I am using this code, and my log always is (null):

self.mutarray = [[NSMutableArray alloc] init];
[self.mutarray addObject:path]; //path is an NSString
NSLog(@"%@", self.mutarray);

It must add the path of the UITableViewCell to the NSMutableArray, is that even possible?

Oh, and I am calling this in - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

2
  • 2
    Have you alloced and initied mutarray? Commented Aug 25, 2013 at 18:12
  • 1
    You never actually created the array. Declaring the array pointer does not create the array. Commented Aug 25, 2013 at 18:22

4 Answers 4

3

As per your comments "it shows only the new added string, but not all strings.". This is because you are allocating NSMutable Array in the delegate method of table view. Everytime you hit the cell a new array is get allocated.

Either allocate your "mutarray" array in ViewDidLoad

Or

Only allocate it if it is nil or empty.

  if(!self.mutarray)
   self.mutarray = [[NSMutableArray alloc] init];
Sign up to request clarification or add additional context in comments.

3 Comments

OK, I added self.mutarray = [[NSMutableArray alloc] init]; to -(void)viewDidLoad, but it still only shows the newest string, and not the newest and the other, previews strings
can you please post your updated code in the - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
OMG, I fixed it, sorry for that. You were completely right, I forgot to delete the alloc init path from the didSelectRowAtIndexPath method. Its 1:09 here, so I think I have to sleep, so I can think better:) Thanks again!
2

It seems that you haven't initialized your array. A good practice for properties is using something called lazy initialization.

Add this method.

- (NSMutableArray *)mutarray
{
    if (!_mutarray) {
        _mutarray = [[NSMutableArray alloc] init];
    }
    return _mutarray;
}

So every time you request the array, the accessor will make sure that the object is created before returning it. This is a good way of defensive programming.

1 Comment

Sorry, I had it initialized and allocated, I just forgot to add it, and it was in the if statement above. Now it shows only the new added string, but not all strings.
1

You have to alloc/init before adding any objects:

if(!mutarray)mutarray = [[NSMutableArray alloc]init];
[self.mutarray addObject:path];

1 Comment

Sorry, I had it initialized and allocated, I just forgot to add it, and it was in the if statement above. Now it shows only the new added string, but not all strings.
0

Have you tried to add a copy of the path? Since UITableViewCells are resused, maybe the problem resides to this?

[self.mutarray addObject:[path copy]];

1 Comment

stupid question: are you doing the init and alloc stuff in didSelectRowAtIndexPath as well? If yes, try to do this part of your code in viewWillAppear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.