【问题标题】:Multiple UITableViews on one UIView一个 UIView 上的多个 UITableViews
【发布时间】:2009-09-12 22:57:19
【问题描述】:

我需要在一个 UIView 上有两个 UITableView。我可以让它与一个一起工作,这是代码:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [contentOne count];  // sets row count to number of items in array
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }

    NSString *firstValue = [[NSString alloc] initWithFormat: @"Row %i% %", indexPath.row+1 ];
    NSString *secondValue = [contentOne objectAtIndex:indexPath.row];

    NSString *cellValue = [firstValue stringByAppendingString: secondValue]; // appends two strings

    [cell.textLabel setText:cellValue];



    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

我尝试了几种不同的方法。任何人?如果我可以为每个 UITableView 命名一个不同的名称,应该这样做,但它不会让我将 tableView 编辑为任何其他内容而不会崩溃。

【问题讨论】:

标签: iphone uitableview


【解决方案1】:

所以你需要一些方法来区分这两个tableViews——你可以将“tag”属性设置为不同的值,或者在你的视图控制器上设置一个指向每个视图的属性

@property (nonatomic, retain) IBOutlet UITableView *tableView1;
@property (nonatomic, retain) IBOutlet UITableView *tableView2;

然后将它们连接到界面构建器中的每个视图...

然后在你的视图控制器方法中你可以做

(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if (tableView == self.tableView1) {
        return 37;
    } else if (tableView == self.tableView2) {
        return 19;
    } else {
        // shouldn't get here, use an assert to check for this if you'd like
    }
}

【讨论】:

  • 我试过 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { if (tableView == self.tableOne){ return 1; } else if (tableView == self.tableTwo){ return 1; } 其他 { 返回 1;它给了我错误本地声明 tableView 隐藏实例
  • 你有一个名为tableView的类变量吗?你这里的类是从 UITableViewController 派生的,还是其他的?
  • 我在 UIView 中做了所有事情。我没有单独的 UITableView 控制器。我只是尝试了部分,这比尝试制作两个单独的 UITableView 容易得多。我能够将我的两个数组分成单独的部分。谢谢你的帮忙。我只是不明白。
  • 如果您希望它们可以单独滚动,那么单独的 tableViews 会很好......如果您想要一个包含两种内容的列表,那么一个列表中的两个部分是要走的路。它最终会开始变得有意义!
【解决方案2】:

实现这一点的最简单方法可能是拥有两个委托和数据源类,一个用于每个表视图。这将减少视图控制器代码中 if (tableview == tableview1) 出现的次数。

【讨论】:

  • 您能提供一个示例实现吗?
  • 但它会成倍增加要实现的委托和数据源方法的数量。这是一个给予和接受的场景:)
【解决方案3】:

This sample code 可能会帮助你...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多