ios - ipad 如何在一个 View 上以编程方式创建多个表

标签 ios ipad uitableview

我需要创建一个 iPad 应用程序,我必须在一个 View 中显示多个表(没有网格,只有 1 个多行的列)。这必须以编程方式完成,因为在后台有人要设置该表数。

View 将有一个滚动条,这样我就可以看到所有的 View 。

这能做对吗?

有人可以提供我的一些代码或链接到一些教程,了解如何在一个 View 中创建 N 个表,以便在我需要时定位它们。

最佳答案

这绝对可以做到。

可能最简单的方法是继承 UITableView,这样您创建的每个 TableView 都可以为其委托(delegate)和数据源拥有唯一的处理程序,ala:

DynamicTableView.h

@interface DynamicTableView : UITableView <UITableViewDelegate, UITableViewDataSource> {
    NSMutableArray *items;
}

@end

动态表格 View .m

#import "DynamicTableView.h"

@implementation DynamicTableView

-(id) initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if (self == [super initWithFrame:frame style:style]) {
        items = [[NSMutableArray alloc] initWithObjects:[NSString stringWithFormat:@"%i", [NSDate timeIntervalSinceReferenceDate]],
                 [NSString stringWithFormat:@"%i", [NSDate timeIntervalSinceReferenceDate]], nil];
    }

    return self;
}

-(void) dealloc {
    [items release];

    [super dealloc];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [items count];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = [items objectAtIndex:indexPath.row];

    return cell;
}

@end

这是一个非常简单的实现,当它初始化时用两个时间戳填充它的数据源(items 数组)。使用它就像这样简单:

for (int i = 0; i < 4; i++) {
    DynamicTableView *table = [[[DynamicTableView alloc] initWithFrame:CGRectMake(10, (i * 100) + 10, 200, 50) style:UITableViewStylePlain] autorelease];
    [table setDelegate:table];
    [table setDataSource:table];

    [self.view addSubview:table];
}

修改 DynamicTableView 以接受您想要的任何数据源及其显示方式。

希望对您有所帮助!

关于ios - ipad 如何在一个 View 上以编程方式创建多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8066144/

相关文章:

ios - 为什么 Swift 不提供类成员初始化器?

c++ - 如何处理完全弃用 AudioSession API 的 C++ 代码?

ios - xcode Storyboard不适合 iphone 5

android - 纯 CSS slider 在移动浏览器中不起作用

ios - 在 UITableView 中用手指在屏幕上绘图

iOS 9 Sprite Kit 自定义着色器似乎被忽略

javascript - 在 iPad 上触发 jQuery mouseout 事件

ios - 如何使用 iOS 企业帐户向客户 (B2B) 分发 iPad 应用程序

ios - 在 iOS 8.3 上注册 Nib/Class 单元崩溃

swift - 如何快速从 UITableView 中删除部分中的页眉 View 和页脚 View ?