多个UITableview索引栏



我在代码中的视图中添加了两个UITableView。我正在正确地将委托和数据源设置为self。我已经添加了所有用于返回行数、行高、节数等的委托方法。一切都很好。我还在两个表中都添加了索引栏。现在的问题是索引栏对第一个表不起作用,而对第二个表起作用很好。

当我点击第一个表索引栏上的任何字符时,它会对第二个表做出响应。我不能参加第一桌的比赛。我还注意到,如果我不将第二个表添加到我的视图中,那么我就可以获得第一个表的操作。

这是我的代码

- (void)viewDidLoad
{
    accountsTable = [[UITableView alloc] initWithFrame:CGRectMake(0,27, 320, 390)     style:UITableViewStylePlain];
    [accountsTable setDelegate:self];
    [accountsTable setDataSource:self];
    [self.view addSubview:accountsTable];
    accountsTable.backgroundColor = [UIColor clearColor];
    [accountsTable release];
    keyConnectionsTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 27, 320, 390) style:UITableViewStylePlain];
    [keyConnectionsTable setDelegate:self];
    [keyConnectionsTable setDataSource:self];
    [keyConnectionsTable setBackgroundColor:[UIColor clearColor]];
    [keyConnectionsTable setHidden:YES];
    [self.view addSubview:keyConnectionsTable];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return [NSArray arrayWithArray:[[UILocalizedIndexedCollation currentCollation] sectionIndexTitles]];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
    return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index]; 
}

您需要区分两个表视图。要做到这一点,您可以简单地使用"tag"属性并将其设置为不同的值,或者您可以在视图控制器中为每个TableView设置一个@property

@property (strong) IBOutlet UITableView *tv1;
@property (strong) IBOutlet UITableView *tv2;

对于您的方法,您可以执行以下操作:

- (NSInteger)numberOfSectionsInTableView:(UITableView *) tableView {
    if (tableView == self.tv1) {
        return 1;
    } else if (tableView == self.tv2) {
        return 2;
    }
}

底线

你需要区分两个TableViews,否则你会弄得一团糟:)

最新更新