iOS - 具有多个表格 View 单元格

标签 ios objective-c uitableview tableview

我的 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/

相关文章:

iphone - UITableView 末尾下方的幻影 "cells"

ios - 更改静态 UITableViewCell 的高度以适合内容

ios - 编译GnuPG for iOS Simulator + iOS时出错

ios - 使用 RestKit 发布多个对象(作为 JSON 数组)

objective-c - 如何在 Objective-C 中将字符串转换为整数?

iphone - iPad : how to change background color for Ipad app?

ios - UITableView 崩溃,因为 "Attempt to create two animations for cell"(iOS 7)

ios - PFQuery 只返回 100

ios - UISearchController 不要将 viewcontroller 推到前面而是在 UISearchController 下面

如果 IOS7 subview 的父 View 正在设置动画,则 subview 会设置动画