我已经在 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/