简体   繁体   English

一个视图中有多个TableView-数据源数组?

[英]Multiple TableViews in one View - Array of data sources?

I'm trying to recreate a view controller similar to the "weather" app that comes stock on the iPhone. 我正在尝试重新创建类似于iPhone上附带的“天气”应用程序的视图控制器。 I've got a scrollview that contains an individual tableview per page, and I'm struggling to figure out a way to specify the data for each table view. 我有一个滚动视图,其中每页包含一个单独的表格视图,并且我正在努力寻找一种为每个表格视图指定数据的方法。

The tricky part is that the info in each tableview and the tableviews themselves change based on user defaults. 棘手的是,每个表视图中的信息以及表视图本身的更改都基于用户默认值。 Basically, I have an Array of dictionaries stored in user defaults, and each object in this array has its own tableview. 基本上,我有一个存储在用户默认值中的字典数组,并且该数组中的每个对象都有其自己的表视图。 Each dictionary contains a title, a lat and a long. 每个字典都包含一个标题,一个经纬度和一个长经​​。 When I create the tables, I also use the lat and long to get some data from the internet via an api call and parser. 创建表时,我还使用lat和long通过api调用和解析器从Internet上获取一些数据。 Here's the code: 这是代码:

    - (void)setupScrollView
{
    scrollView.delegate = self;

    [self.scrollView setBackgroundColor:[UIColor clearColor]];
    [scrollView setCanCancelContentTouches:NO];

    scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    scrollView.clipsToBounds = YES;
    scrollView.scrollEnabled = YES;
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;

    //this is an array of dictionaries that hold a location title, as well as a lat and lng.
    NSArray *arrayForLocations = [NSArray arrayWithArray:[appDelegate.defaults objectForKey:@"arrayOfLocationDicts"]];
    NSLog(@"Array of Location Dicts holds: %@",arrayForLocations);

    for (int i = 0; i < arrayForLocations.count; i++) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width * i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;

        UIView *subview = [[UIView alloc] initWithFrame:frame];
        [self.scrollView addSubview:subview];
        UITableView *tbView = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, scrollView.frame.size.width, scrollView.frame.size.height - 40)];
        tbView.backgroundColor = [UIColor grayColor];
        tbView.tag = i;
        tbView.delegate = self;
        tbView.dataSource = self;
        [subview addSubview:tbView];

        UILabel *locationLabel = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 260, 40)];
        locationLabel.textColor = [UIColor blackColor];
        locationLabel.text = [[arrayForLocations objectAtIndex:i]objectForKey:@"location_address"];
        [subview addSubview:locationLabel];

        pageControl.numberOfPages = [arrayForLocations count];
        //get coordinate from dictionary
        CLLocationCoordinate2D eventCoordinate;
        eventCoordinate.latitude = [[[arrayForLocations objectAtIndex:i]objectForKey:@"location_latitude"]floatValue];
         eventCoordinate.longitude = [[[arrayForLocations objectAtIndex:i]objectForKey:@"location_longitude"]floatValue];
        //turn coordinate into data
        SDJConnection *connection = [[SDJConnection alloc]init];
        NSMutableArray *singleDataSource = [connection getEventInfoWithCoordinate:eventCoordinate];
        //store data in array
        [arrayOfDataSources addObject:singleDataSource];
        NSLog(@"array of Data Sources in the scrollsetup: %@",arrayOfDataSources);

    }

    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * arrayForLocations.count, self.scrollView.frame.size.height);
}

So now my problem is telling each of these tables what data to display. 所以现在我的问题是告诉每个表要显示哪些数据。 My first thought was to set the tag of the table, as I do above, and then have something like this in the cellForRowAtIndexPath - 我首先想到的是像上面一样设置表的标签,然后在cellForRowAtIndexPath中添加类似的内容-

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        }

         int i = tableView.tag;
        if (tableView.tag = i) {

            cell.textLabel.text = [[[arrayOfDataSources objectAtIndex:i]objectAtIndex:indexPath.row]eventTitle];
    }

that unfortunately hasn't been working for me correctly. 不幸的是,这一直没有为我正确地工作。 Does anyone have any thoughts as to how to get this done? 是否有人对如何完成这项工作有任何想法?

Thanks!! 谢谢!!

For sure 当然

if (tableView.tag = i) {

should be 应该

if (tableView.tag == i) {

and even then, it's an odd construct -- not sure what you're trying to achieve there. 即便如此,这也是一个奇怪的构想-不确定您要达到的目标。 You set the i to the tag and then you check to see if the tag is i? 您将i设置为标签,然后检查标签是否为i? Doesn't really make sense -- that's where I'd suggest you take a close look and rethink your logic. 真的没有道理-那是我建议您仔细看看并重新考虑逻辑的地方。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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