一个 UIView 上的多个 UITableView [英] Multiple UITableViews on one UIView

查看:25
本文介绍了一个 UIView 上的多个 UITableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to have two UITableViews on one UIView. I can make it work with one, here is the code:

- (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 编辑为其他任何内容.

I have tried several different methods. Anyone? If I could name each UITableView a different name that should do it but it will not let me edit tableView to anything else without crashing.

推荐答案

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

so you need some way to tell the two tableViews apart--you could either set the "tag" property to different values, or have a property on your view controller that points to each view

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

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

then hook these up to each view in interface builder...

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

then in your view controller methods you can do

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

这篇关于一个 UIView 上的多个 UITableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆