0

我正在尝试重新创建一个类似于 iPhone 上的“天气”应用程序的视图控制器。我有一个滚动视图,每页包含一个单独的表格视图,我正在努力寻找一种方法来为每个表格视图指定数据。

棘手的部分是每个 tableview 中的信息和 tableviews 本身会根据用户默认值发生变化。基本上,我有一个存储在用户默认值中的字典数组,并且该数组中的每个对象都有自己的表格视图。每个字典都包含一个标题、一个纬度和一个经度。创建表时,我还使用 lat 和 long 通过 api 调用和解析器从 Internet 获取一些数据。这是代码:

    - (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);
}

所以现在我的问题是告诉这些表中的每一个要显示什么数据。我的第一个想法是设置表格的标签,就像我在上面做的那样,然后在 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];
    }

不幸的是,这并没有为我正确工作。有人对如何完成这项工作有任何想法吗?

谢谢!!

4

1 回答 1

0

当然

if (tableView.tag = i) {

应该

if (tableView.tag == i) {

即便如此,这也是一个奇怪的结构——不确定你想要在那里实现什么。您将 i 设置为标签,然后检查标签是否为 i?真的没有意义——这就是我建议你仔细看看并重新考虑你的逻辑的地方。

于 2012-04-20T21:04:57.893 回答