简体   繁体   中英

UITableView with NSMutableArray

I'm populating a UITableView based on the values of an NSMutableArray. This table view has search results. If the user clicks in one of the results, one will navigate to another screen. If the user clicks “back”, the search results are filled in again. At this point, while the table view is being repopulated, the old values still appear, just as I want. However, since I'm doing:

 - (void)viewWillAppear:(BOOL)animated
{
    NSMutableArray *m = [[NSMutableArray alloc] init];
    self.searchResultsArray = m;
    [m release];
}

The old cells information is no longer available. Thus, the app crashes if the user clicks in one of the old cells or scrolls the UITableView because I'm accessing the mutable array which was reinitialized above.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *cellArray = [searchResultsArray objectAtIndex:indexPath.row];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    appDelegate.selectedCell = [searchResultsArray objectAtIndex:indexPath.row];
}

Do you have any suggestions concerning how should I do this properly?

Thanks.

It seems to me that you are not initialising the NSArray in the right method ( viewDidLoad: ). Using XLData you don't have to care about where you set up the storage, reload the UITableView or add items to the NSArray (searchResultsArray) since it keeps track of the data (NSArray) and updates the UITableView accordingly and on the fly.

You should reset the array only when the view loads:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSMutableArray *m = [[[NSMutableArray alloc] init] mutableCopy];
    self.searchResultsArray = m;
    [m release];
}

You should always call the reloadData: method when changing the data in the UITableViewDataSourceDelegate instance.

Let me know if this helps

this is because you are re-initalizing the array each time the view comes back in focus, the array will then have 0 objects in, causing the issue when you select a row and reference an index in the array that simply was wiped when the viewWillAppear is called.

why not init ' self.searchResultsArray ' in viewDidLoad (remembering to undo this with release when the device receives memory warning)

let me know how you get on.

Please try below code:

- (void)viewWillAppear:(BOOL)animated
{
    // Your search result array with your old values
    // Now add or appened new data to your old search results.

    [self.searchResultsArray addObject:@"Your Value 1"];
    [self.searchResultsArray addObject:@"Your Value 2"];
    [self.searchResultsArray addObject:@"Your Value 3"];
    [self.tableView reloadData];

  //  if you need to add new values from a array put this code in a loop.   
}

It should helps you...

Thx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM