iphone - 一个 UIView 上有多个 UITableView

标签 iphone uitableview

我需要在一个 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 编辑为其他任何内容而不崩溃。

最佳答案

所以你需要某种方法来区分两个tableView——你可以将“tag”属性设置为不同的值,或者在 View Controller 上有一个指向每个 View 的属性

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

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

然后在你的 View Controller 方法中你可以这样做

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

关于iphone - 一个 UIView 上有多个 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1416372/

相关文章:

json - 当对象具有相同的值时,如何在我的 tableView 中显示 JSON 数据?

iphone - 有什么办法只能通过蓝牙连接GKSession吗?

ios - 猜字游戏的股票代码/框架-键盘 UIView

iphone - 在 UITableView 中使用多种单元格样式类型和自定义单元格

xcode - 读取 UITableView Section Title 的字符串值

iphone - 从 UITableView 创建 UIImage

objective-c - 在 iphone sdk 中滑动以删除和移动 UItableView 选项中的单元格

ios - iOS 11 上 UITableView 中的 UISearchController 问题

iphone - Cocos2d/Box2d CCRibbon 碰撞检测

iphone - 在 Xcode 中将 iPhone 目标应用程序转换为 Universal