【问题标题】:ipad how to create programmatically multiple tables on one viewipad如何在一个视图上以编程方式创建多个表
【发布时间】:2011-11-09 14:03:31
【问题描述】:

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

视图会有一个滚动条,所以我可以看到所有这些。

这可以做对吗?

有人可以提供我的一些代码或链接到一些关于如何在一个视图中创建 N 数量表的教程,以便随时定位它们。

【问题讨论】:

    标签: ios ipad uitableview


    【解决方案1】:

    这绝对可以做到。

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

    DynamicTableView.h

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

    DynamicTableView.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 以接受您想要的任何数据源及其显示方式。

    希望有帮助!

    【讨论】:

    • 谢谢!! idd 我需要不止一种类型的表格视图。我需要视频、照片、纯文本和其他一些东西。现在,我可以在一个视图中创建表,如果我制作 150k 表,我该如何滚动视图?这不是一个好的做法吗?我的问题是我需要类似于 hitpad 应用程序的东西。提前谢谢
    • 滚动视图没问题。您需要做的是跟踪您添加了多少表视图并相应地设置您的内容大小。例如,在初始化表的循环之前,将整数设置为 0。在循环内部,当每个表被创建时,将整数设置为 += table.frame.origin.y。循环后,设置滚动视图的内容视图,使用整数作为高度的参数!
    • 老兄,正是我所需要的:D 只需要创建自定义单元格,我就可以使用它了。 thx thx thx thx thx thx thx !!!
    • 我相信使用不同的控制器作为代表是一个更好的解决方案。子类化使其更加模糊,并增加了 Objectville 中所谓的“智力距离”:)
    【解决方案2】:

    根据您的描述,我假设您希望拥有任意数量的表格,所有表格都位于一个本身可滚动的视图上(以便您向上或向下滚动以访问所有表格)。这在 iOS 中是非常不可取的,因为表格视图本身是可滚动视图的子类,您将在让各个表格与“主”可滚动视图一起正确滚动时遇到重大问题。

    假设这是您想要做的,您最好使用拆分为多个部分的单个表视图。这是a good tutorial,它显示了如何执行此操作。

    【讨论】:

    • 更明确地说,我的老板喜欢 hitpad 应用程序和他们展示主视图的乳清。知道它们是如何产生这种效果的吗?
    【解决方案3】:

    我希望下一个代码可以成为您的起点:

    @interface MyController : UIViewController <UITableViewDelegate, UITableViewDataSource>
    
    @property (nonatomic, retain) UITableView *table1;
    @property (nonatomic, retain) UITableView *table2;
    
    @end
    
    @implementation MyController
    
    @synthesize table1 = _table1,
                table2 = _table2;
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        CGRect table1Rect = CGRectMake(0, 0, 200, 300);
        UITableView *table1 = [[UITableView alloc] initWithFrame:table1Rect style:UITableViewStyleGrouped];
        table1.delegate = self;
        table1.dataSource = self;
        [self.view addSubview:table1];
        self.table1 = table1;
        [table1 release];
    
        CGRect table2Rect = CGRectMake(200, 0, 200, 300);
        UITableView *table2 = [[UITableView alloc] initWithFrame:table2Rect style:UITableViewStyleGrouped];
        table2.delegate = self;
        table2.dataSource = self;
        [self.view addSubview:table2];
        self.table2 = table2;
        [table2 release];
    }
    
    - (void)viewDidUnload {
        self.table1 = nil;
        self.table2 = nil;
    }
    
    - (void)dealloc {
        self.table1 = nil;
        self.table2 = nil;
        [super dealloc];
    }
    
    @end
    

    【讨论】:

    • 这有帮助,我如何滚动有这 2 个表的主视图?
    猜你喜欢
    • 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
    相关资源
    最近更新 更多