开发者

how to switch arrays from which a uitableview is populated

Hello im constructing an iphone app the requires the user to switch tablevi开发者_C百科ews depending on which button is selected at the top. I have five different tableviews each populated from an array in a plist file. My question is how do i tell this uitableview to switch to another array. Thanks!


You can have a dictionary or array of your arrays (the data) and a property/iVar for the "current" array. When they select a different option, you change the value of current array and call [tableView reloadData]; That will cause the table view call backs to trigger and reload all the data. All the table view callbacks should get their data from current array.

For example, let's say we had three data sets, "cars", "computers", and "devices".

    // defined as property in header to handle retain/release
    @property (retain) NSArray *current;

    // construct your data on load or init
    NSArray *cars = [NSArray arrayWithObjects:@"porsche", @"corvette", @"pacer", nil];
    NSArray *computers = [NSArray arrayWithObjects:@"PC", @"iMac", nil];
    NSArray *devices = [NSArray arrayWithObjects:@"iPhone", @"iPad", @"iPod", nil];

    NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
    [data setObject:cars forKey:@"cars"];
    [data setObject:computers forKey:@"computers"];
    [data setObject:devices forKey:@"devices"];

    // when they select computers, change the current array to computers array
    [self setCurrent: [data objectForKey:@"computers"]];

    // since you changed which dataset to use, trigger for the table view to reload.
    [tableView reloadData];

    // all table view callbacks work off of current array


Your tableView datasource returns values for the number or sections and rows in sections, and UITableViewCell for each row. All you need to do is make sure the data from the appropriate array is used to return the correct values.

For example, if you have 5 arrays (array1, array2, etc.), so could also declare another array property to which you assign the array from which you want to return data:

self.dataArray = self.array1 , say, when the first button is pressed

then use self.dataArray to return values in your datasource methods.


On button action perform this way.

 -(void)ButtonPressed:(id)sender {

    switch([sender tag]) {

    case 0: {
    self.resultArray = [NSArray arrayWithObjects:@"ABC",@"MNO",nil];
//self.resultArray = //sameArrayAssigned to it
    break;
    }

    default:
    break;
    }
    [self.tableView reloadData];

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜