ios - UITableViewController 中的多个自定义原型(prototype)单元格

标签 ios uitableview cell

我已经在 Internet 上搜索了大约两天,但我似乎无法理解它是如何工作的。

我有两个视频单元 FeaturedVideoCell 和 YouTubeVideoCell。将只有一个 FeaturedVideoCell 和多个 YouTubeVideoCells...

我正在运行数据获取算法,将获取填充两个单元格的数据,然后通过重新加载表格数据来填充单元格。每个单元格根据相应数组中的对象计数填充一定数量的行。

我让它在 YouTubeVideoCells 上工作,但似乎无法让第二个 FeaturedCell 工作。我要么收到错误,要么没有加载我的第一个单元格。我已经尝试了来自该站点和其他站点的多种解决方案,但感到非常沮丧。有谁知道正确的实现技术吗?我也在使用 Storyboard...

// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(isFiltered)
{
    return filteredyoutuberArray.count;
}
else
{
    return youtuberArray.count;
}
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"videoCell";
YouTubeVideoCell *cell = nil;

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
    cell = [[YouTubeVideoCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

YouTubeVideoObj* tempVideo = [[YouTubeVideoObj alloc] init];
if(isFiltered == TRUE)
{
    tempVideo = [filteredyoutuberArray objectAtIndex:indexPath.row];
}
else
{
    tempVideo = [youtuberArray objectAtIndex:indexPath.row];
}

cell.YouTuberName.text = tempVideo.YouTuberName;
cell.totalUploadViews.text = tempVideo.totalUploadViews;
cell.subscribers.text = tempVideo.subscribers;
cell.videoCount.text = tempVideo.videoCount;
cell.orderNum.text = tempVideo.orderNum;
cell.profile.image = tempVideo.profile;

[cell setNeedsLayout];
[cell setNeedsDisplay];
[cell prepareForReuse];

return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}

最佳答案

啊。我想我知道发生了什么......你有两个不同的 UITables 吗? 如果是这样,无论选择/触摸/调用/填充哪个单元格/表格等,都会触发相同的方法 cellForRowAtIndexPath。

如果这是您的问题,请尝试检测当前正在调用该方法的表,然后在每种情况下执行适当的操作。例如:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

// blah blah cell stuff 

if(tableView == YouTubeTable) {
  // assign relevant data to relevant table based on relevant row
}
else if (tableView == FeaturedTable) {
  // ditto but with different relevance
}

}

???

关于ios - UITableViewController 中的多个自定义原型(prototype)单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24668342/

相关文章:

iOS:如何在不重新启动应用程序的情况下以编程方式更改应用程序语言?

ios - 用 if var 解开并仍然返回单元格

javascript - 即使在添加控制台插件后,console.log 在 iOS Phonegap App 中也不起作用

ios - 有时我的 UITextField 对触摸没有响应,为什么?

ios - 通知 UITableViewCell tableview 正在滚动的最佳方法是什么

ios - 表格单元格中的 Firebase 观察者是否算作每个实例的观察者?

c# - 在编辑其值时访问 Datagridview 单元格值

ios - 当您按下按钮时,在 Swift 中的 UicollectionViewController 中添加单元格

ios - Xcode 调试器 : fatal error: Array index out of range. 。为什么?

ios - 使用 Google Sign-In SDK 和 Azure iOS SDK 执行客户端身份验证