我的 xcode 项目有点问题。基本上,我尝试使用两个 View Controller ,每个 Controller 都有自己的 TableView 。假设第一个有文章列表,第二个有视频列表。
在我的 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 错误。
帮助?抱歉,如果我没有提供足够的信息来提供适当的建议。这是我第一次问这个问题。
最佳答案
委托(delegate)方法称为 tableView: cellForRowAtIndexPath: 方法 tableView2: cellForRowAtIndexPath: 不是委托(delegate)方法。它永远不会被调用。
在 tableView:numberOfRowsInSection: 和 tableView: cellForRowAtIndexPath: 中你需要做的是查看 tableView 参数,看看它是你的两个 tableView 中的哪一个。我可能有两个属性,一个用于 tableView1 和 tableView2(调用第一个 tableView1 而不是 tableView2 将避免一些混淆),并检查一下
if (tableView == self.tableView1)
{
...
}
else if (tableView == self.tableView2)
{
....
}
另请查看您的用户界面,看看您是否不能将一个 TableView 与两个部分一起使用。
关于iOS - 具有多个表格 View 单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30006069/