菜鸟教程小白 发表于 2022-12-11 20:28:52

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


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

<p> View 会有一个滚动条,所以我可以看到所有这些。</p>

<p>这可以做对吗?</p>

<p>有人可以提供我的一些代码或链接到一些关于如何在一个 View 中创建 N 个表的教程,以便随时定位它们。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>这绝对可以做到。</p>

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

<p>DynamicTableView.h</p>

<pre><code>@interface DynamicTableView : UITableView &lt;UITableViewDelegate, UITableViewDataSource&gt; {
    NSMutableArray *items;
}

@end
</code></pre>

<p>DynamicTableView.m</p>

<pre><code>#import &#34;DynamicTableView.h&#34;

@implementation DynamicTableView

-(id) initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if (self == ) {
      items = [ initWithObjects:],
               ], nil];
    }

    return self;
}

-(void) dealloc {
    ;

    ;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return ;
}

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

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

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

    cell.textLabel.text = ;

    return cell;
}

@end
</code></pre>

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

<pre><code>for (int i = 0; i &lt; 4; i++) {
    DynamicTableView *table = [[ initWithFrame:CGRectMake(10, (i * 100) + 10, 200, 50) style:UITableViewStylePlain] autorelease];
    ;
    ;

    ;
}
</code></pre>

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

<p>希望有帮助!</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - ipad如何在一个 View 上以编程方式创建多个表,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/8066144/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/8066144/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - ipad如何在一个 View 上以编程方式创建多个表