0

iPhone に付属する「天気」アプリに似たビュー コントローラーを再作成しようとしています。ページごとに個別のテーブルビューを含むスクロールビューがあり、各テーブル ビューのデータを指定する方法を見つけるのに苦労しています。

注意が必要なのは、各テーブルビューの情報とテーブルビュー自体がユーザーのデフォルトに基づいて変化することです。基本的に、ユーザーのデフォルトに辞書の配列が格納されており、この配列内の各オブジェクトには独自のテーブルビューがあります。各辞書には、タイトル、緯度と経度が含まれています。テーブルを作成するときは、緯度と経度も使用して、API 呼び出しとパーサーを介してインターネットからデータを取得します。コードは次のとおりです。

    - (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 に答える