iOS - 有多个 Table 视图单元格

iOS - Having multiple Table View Cells

我的 xcode 项目有点问题。基本上,我正在尝试使用两个视图控制器,每个视图控制器都有自己的 Table 视图。假设第一个有文章列表,第二个有视频列表。

在我的 ViewController.m 文件中我有:

#import "ViewController.h"
#import "CustomCell.h"

@interface ViewController ()

@end

@implementation ViewController

static NSString *CellIdentifier = @"CellTableIdentifier";


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _articles = @[
            @{@"Headline": @"Jeff", @"subHeadline": @"swaag"},
            @{@"Headline": @"Jeff", @"subHeadline": @"164"},
            @{@"Headline": @"Jeff", @"subHeadline": @"124"},
            @{@"Headline": @"Jeff", @"subHeadline": @"185"},
            @{@"Headline": @"Jeff", @"subHeadline": @"123"},
            @{@"Headline": @"Jeff", @"subHeadline": @"194"},
            @{@"Headline": @"Jeff", @"subHeadline": @"249"},
            @{@"Headline": @"Jeff", @"subHeadline": @"784"},
            ];

    _videos = @[
            @{@"Headline": @"Jeffers", @"subHeadline": @"swag"}
            ];


UITableView *tableView = (id)[self.view viewWithTag:1];
[tableView registerClass:[CustomCell class] forCellReuseIdentifier:CellIdentifier];

UITableView *tableView2 = (id)[self.view viewWithTag:2];
[tableView registerClass:[CustomCell class] forCellReuseIdentifier:CellIdentifier];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.articles count];
}

- (NSInteger)tableView2:(UITableView *)tableView2 numberOfRowsInSection:(NSInteger)section {
    return [self.videos count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSDictionary *rowData = self.articles[indexPath.row];
    cell.headline = rowData[@"Headline"];
    cell.subHeadline = rowData[@"subHeadline"];

    return cell;
}

- (UITableViewCell *)tableView2:(UITableView *)tableView2 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomCell *cell = [tableView2 dequeueReusableCellWithIdentifier:CellIdentifier];
    NSDictionary *rowData = self.videos[indexPath.row];
    cell.headline = rowData[@"Headline" ];
    cell.subHeadline = rowData[@"subHeadline"];

    return cell;
}

@end

我有 tableview2,因为我试图只在我的第二个 ViewController 中使用视频。即使我更改标签,我也会收到线程 1:信号 SIGABART 错误。

帮忙?抱歉,如果我没有提供足够的信息来提供适当的建议。这是我第一次问这个问题。

调用委托方法 tableView: cellForRowAtIndexPath: 方法 tableView2: cellForRowAtIndexPath: 不是委托方法。它永远不会被调用。

你需要在 tableView:numberOfRowsInSection: 和 tableView: cellForRowAtIndexPath: 中做的是查看 tableView 参数并查看你的两个 table意见是。我可能有两个属性,一个用于 tableView1 和 tableView2(调用第一个 tableView1 而不是 tableView2 会避免一些混淆),然后检查

if (tableView == self.tableView1)
{
    ...
}
else if (tableView == self.tableView2)
{
    ....
}

另请查看您的用户界面,看看是否不能将一个 table 视图用于两个部分。

我建议您为每个 tableView 设置一个单独的 UIViewController。通常,viewController、委托(以及数据源)都在同一个 class 中。一次 class 中的一个 table 可能有很多代码。因此,您应该将其拆分。如果你这样做并且你正在使用带有 UIViewControllers 的故事板,你的整个屏幕将需要一个 UIViewController 本身。存在类似 "Container" 的内容,您可以在故事板中为子 viewController 选择。

此外,您对标签的评论:不要使用它们再次查找您的视图(除非您处于某种可能需要重构的 "generic magic" 代码中)。您应该为此使用 IBOutlet。