所以我试图在一个 View 中制作两个表格 View ,但我遇到了一些麻烦。 我已经阅读了一些关于如何执行此操作的其他回复,但它们并没有完全帮助我。
在我的 .h 文件中,我为两个 View 创建了两个导出,分别称为 myFirstViewText
和 mySecondViewTex
所以在我的 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/