ios - 一个 Controller 中的两个 TableView

标签 ios xcode uitableview

所以我试图在一个 View 中制作两个表格 View ,但我遇到了一些麻烦。 我已经阅读了一些关于如何执行此操作的其他回复,但它们并没有完全帮助我。

在我的 .h 文件中,我为两个 View 创建了两个导出,分别称为 myFirstViewTextmySecondViewTex

所以在我的 m 文件中 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

我希望能够在每个不同的 Controller 中打印出单独的值,但我不太确定,因为您只返回 1 个单元格?

到目前为止我已经完成了

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Rx";
static NSString *CellIdentifier2 = @"allergies";
UITableViewCell *cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CellIdentifier];
UITableViewCell *cell2 = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CellIdentifier2];
if(!cell){
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (!cell2) {
    cell2 = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier2];
}
if (tableView == self.myFirstTextView ) {
       cell.textLabel.text = @"HI JAZZY";//[RxDict objectAtIndex:indexPath.row];
}
if (tableView == self.mySecondTextView) {
      cell.textLabel.text = @"BYE JAZZY";//[RxDict objectAtIndex:indexPath.row];
}
tableView = self.mySecondTextView;
cell2.textLabel.text = @"I love Jazzy :D";
return cell2;

这会在我的第一个 TableView 中打印“I love Jazzy”,而在第二个 TableView 中不会打印任何内容。为什么会发生这种情况,我该如何解决? 谢谢:D

最佳答案

所有将您的类的实例设置为数据源的 tableView 都会调用此方法。

这意味着您需要检查哪个 tableView 正在询问某某单元格编号。

因此,您的方法基本上应该如下所示:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == rxTableView) {
        //prepare and return the appropriate cell for *this* tableView
    }
    else if (tableView == allergiesTableView) {
        //prepare and return the appropriate cell for *this* tableView
    }
    return nil; //because you don't expect any other tableView to call this method
}

关于ios - 一个 Controller 中的两个 TableView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13440899/

相关文章:

ios - 获取单元格的 indexPath.row

ios - 在委托(delegate)中调用 Viewcontroller 时,应用程序在 iPhone 5 上崩溃

ios - 调用函数后如何导航到不同的内容页面

ios - swift + 核心数据 : Cannot Automatically Set Optional Attribute On Generated NSManagedObject Subclass

php - Xcode 7 beta swift 使用未解析的标识符

ios - 当我进入 View 控件时,如何更改 3 段控件段的标题?

ios - PHCollection : Fetch all albums, 按最近编辑排序

ios - Swift3 中位置搜索的完成处理程序

iphone - uitableviewcell 的自定义选择样式颜色

ios - 触摸时未调用 didSelectRowAtIndexPath