IOS:tableview委托两个tableview的方法

Cra*_*Dev 7 xcode delegates uitableview ios

我在类中的tableview中有这些委托方法:

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [array1 count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {


static NSString *CellIdentifier = @"Cell";

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

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

return cell;
}
Run Code Online (Sandbox Code Playgroud)

如果我有一个UITableView它没关系,但如果我有两个UITableView?我该如何组织我的代码?有标签?

Pri*_*yat 13

了解所有委托方法如何包含tableView:(UITableView *)tableView在其中?

您可以在头文件中定义表视图,然后只需执行:(假设您的表被调用myTable)

if (tableView == myTable)
Run Code Online (Sandbox Code Playgroud)

然后,您可以拥有任意数量的表视图.

例如:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

return [array1 count];
}
Run Code Online (Sandbox Code Playgroud)

变为:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (tableView == myTable)
    {
       return [array1 count];
    }
    if (tableView == myTable2)
    {
       return [array2 count];
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)