0
votes

I have a table view with 2 columns displaying contents of 2 mutable arrays respectively.

I added a button to remove the selected row and ultimately the arrays objects at index of selected row. I have the button properly linked to class method:

@implementation TableController

-(IBAction)remove:(id)sender{
    NSInteger row= [tableView selectedRow];
    [_trackNames removeObjectAtIndex:row];
    [_trackNumber removeObjectAtIndex:row];
    [tableView reloadData];
}

The tableView datasource methods

-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView

    return self.trackNames.count;

-(id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {

    if ([tableColumn.identifier isEqualToString:@"Track"]) {
        return [self.trackNumber objectAtIndex:row];
    }
    else{
        return [self.list objectAtIndex:row];
    }
}

When running the app, clicking the remove button does not do anything.

Is the problem due to the presence of two arrays?that the tableView would get conflicted. should I use method - reloadDataForRowIndexes:columnIndexes: for both arrays instead?

Edit

here are the arrays used to populate the table view. (tableView displays their contents correctly, no flaws here I guess)

-(NSMutableArray*)trackNames{

    NSString *pathDataTableView = @"TrackSelectorData/ArrayTableView.plist";

    _trackNames = [NSMutableArray arrayWithContentsOfFile:pathDataTableView];

    return _trackNames;
}

-(NSMutableArray*)trackNnumber{

    _trackNumber = [[NSMutableArray alloc]init];

    for (NSInteger i = 0; i < [_trackNames count]; i++){
        [_trackNumber  addObject:[NSNumber numberWithInteger:i+1]];

    }

    return _trackNumber;
2
What are those 2 mutable arrays that you are referring to? Your code suggests that there are several arrays involved.El Tomato
Hi, I just edited the post, one of the array is filled with names and the other with numbers.Names vary depending on data file. thxuser306147
What are self.list and self.listnumber, then?El Tomato
I have edited the methods names to clarify that, thx. still cant remove rows thoughuser306147

2 Answers

0
votes

I think something wrong with your data source. You query self.list for the row count, but it's populated every time you access self.list.

So when you remove data from array in memory, reloadData just get another unmodified from plist file.

0
votes

The solution courtesy of @reviver:

-(NSMutableArray*)trackNames{

if ([_trackNames objectAtIndex:0 !=NULL]){}

else {


NSString *pathDataTableView = @"TrackSelectorData/ArrayTableView.plist";

_trackNames = [NSMutableArray arrayWithContentsOfFile:pathDataTableView];

}
return _trackNames;

}