0

いくつかのUITableViewを垂直に並べてプロジェクトに取り組んでいます。それらのテーブルを表示する必要がありましたが、テーブルごとに異なるテーブルの内容を表示しようとすると行き詰まりました。これはコードです:

-(void)setTheTable{
    int numberOfTables = 5;

    //height of the table
    CGFloat height = self.view.bounds.size.height;
    CGRect tableBounds = CGRectMake(0.0f, 0.0f, self.widthOfTheScreen, height- 30);

    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 30.0f, self.widthOfTheScreen, height - 30.0f)];

    self.scrollView.contentSize = CGSizeMake(self.widthOfTheScreen * numberOfTables, height-30);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounds = tableBounds;
    [self.view addSubview:self.scrollView];

    //put five tables
    CGRect tableFrame = tableBounds;
    tableFrame.origin.x = 0;
     for (int i = 0; i < numberOfTables; i++) {

        UITableView *tableView = [[UITableView alloc] initWithFrame:tableFrame style:UITableViewStylePlain];
         tableView.delegate = self;
         tableView.dataSource = self;
         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
         if (cell == nil) {
             cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
         }
        switch (i) {
            case 0:
                tableView.backgroundColor = [UIColor yellowColor];
                tableView.tag = 0;
                cell.textLabel.text = @"section = 0 行0";
                break;
            case 1:
                tableView.backgroundColor = [UIColor blueColor];
                tableView.tag = 1;
                break;
            case 2:
                tableView.backgroundColor = [UIColor brownColor];
                tableView.tag = 2;
                break;
            case 3:
                tableView.backgroundColor = [UIColor greenColor];
                break;
            case 4:
                tableView.backgroundColor = [UIColor cyanColor];
                break;
            default:
                break;
        }

        [self.scrollView addSubview:tableView];
        tableFrame.origin.x += self.widthOfTheScreen;

    }
}





- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
    int num = tableView.tag;
    if(num == 1) {
        if(indexPath.row == 0) {
            cell.textLabel.text = @"section = 0 行0";
        } else if(indexPath.row == 1){
            cell.textLabel.text = @"section = 0 行1";
        } else {
            cell.textLabel.text = @"section = 0 行2";
        }
    } else {
        NSString *str = [[NSString alloc] initWithFormat:@"section = %d col %d",indexPath.section,indexPath.row];
        cell.textLabel.text = str;
    }
    return cell;
}

tableview.tagが役立つと思いましたが、5つの同じテーブルが表示されました。この問題を解決するためのアイデアを教えていただけますか?

ありがとうございました。

4

1 に答える 1

1

まず第一に、にを追加することについてのこの答えを見てください、Appleはそれに対して特に警告します(彼らはあなたのせいではなく、ドキュメントに警告を含んでいませんでしたが)。テーブルをサブビューとしてNOTに追加します(これを完全に削除することをお勧めします)。UITableViewUIScrollViewUITableViewUIView UIScrollView

次に、メソッドでのみを作成しUITableViewCellますtableView:cellForRowAtIndexPath:。forループからanyへのすべての参照をUITableCell完全に削除します(それらは使用されていなくても、何の役にも立ちません)。テーブルのデータソースとデリゲートを設定し、[tableView reloadData]各アブルを呼び出すと、データソースメソッドが自動的に呼び出されます。

複数のテーブルで作業しているため、状況は少し複雑になります。クラス内のすべてのテーブルビューへの参照を保持する必要があります(配列内のいずれかですが、理想的には各テーブルビューのプロパティ)。

また、いつでもどのテーブルがロードされているかを知る必要があります。例:

-(UITableViewCell*)tablView:tableView cellForRowAtIndexPath:indexPath {
    UITableViewCell *cell;
    if ( tableView == self.tablView1 ) {
        // build your cells for table view 1
    }
    else if ( tableView == self.tableView2 ) {
        // build cells for table view 2
    }

    return cell;
}

複数のテーブルがある場合、特に各テーブルの背後にあるデータモデルが大幅に異なる場合、および/または各テーブルの各UIが異なる場合は、各テーブルの特定のデリゲートを検討することをお勧めします。

私はあなたの正確なニーズを知りませんが、複数のテーブルの代わりに複数のセクションを持つテーブルも検討します。

また、 iOS TableViewプログラミングガイドを読むことをお勧めします。これは、デリゲートとデータソースについてです。

于 2013-03-05T02:57:37.957 に答える