为什么我的 SearchController 没有为第一个单元格显示正确的单元格原型?
Why is my SearchController not displaying the correct cell prototype for the first cell?
如标题所示,我的 UISearchController 在搜索结果中为第一个单元格显示错误的单元格原型时遇到了问题。
背景信息:我有两个单元格原型,一个没有图像(标识符:basicCell),另一个带有 UIImageView(标识符:imageCell)。单元格在不搜索时工作正常。
问题的详细描述: 当我单击搜索栏时一切正常,直到我开始搜索某些东西。当我这样做时,第一个单元格总是有 imageCell 标识符(显示一个灰色的空图像视图,表示缺少图像),无论如何。注意:在搜索任何内容之前,tableview 中的第一个单元格有一个自定义图像……也许这是值得注意的?
无论如何,我不知道我做错了什么。有人介意帮忙吗?
代码:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if (self.resultSearchController.active) {
if hasImageAtIndexPath(indexPath) {
let cell = tableView.dequeueReusableCellWithIdentifier(imageCellIdentifier, forIndexPath: indexPath) as! TimelineTableViewCellImage
let event = filteredTableData[indexPath.row]
cell.content.text = profile.content
cell.name.text = profile.name
//This is the image
cell.attachment.image = profile.image
cell.attachment.layer.cornerRadius = 1
cell.attachment.clipsToBounds = true
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier(basicCellIdentifier, forIndexPath: indexPath) as! TimelineTableViewCell
let event = filteredTableData[indexPath.row]
cell.content.text = profile.content
cell.name.text = profile.name
return cell
}
} else {
if hasImageAtIndexPath(indexPath) {
let cell = tableView.dequeueReusableCellWithIdentifier(imageCellIdentifier, forIndexPath: indexPath) as! TimelineTableViewCellImage
let event = events[indexPath.row]
cell.content.text = profile.content
cell.name.text = profile.name
cell.attachement.image = profile.image
cell.attachment.layer.cornerRadius = 1
cell.attachment.clipsToBounds = true
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier(basicCellIdentifier, forIndexPath: indexPath) as! TimelineTableViewCell
let event = events[indexPath.row]
cell.content.text = profile.content
cell.name.text = profile.name
return cell
}
}
}
这是我检查图像的代码:
func hasImageAtIndexPath(indexPath:NSIndexPath) -> Bool {
let event = events[indexPath.row]
let imageArray = [event.image]
for eventImage in imageArray {
if eventImage != nil {
return true
}
}
return false
}
您的 hasImageAtIndexPath:
函数中需要有一个 if-else 子句,就像您在 cellForRowAtIndexPath:
中一样。如果 table 视图是搜索 table,则 event
需要按照与 cellForRowAtIndexPath:
、
相同的方式定义
func hasImageAtIndexPath(indexPath:NSIndexPath sender:UITableView) -> Bool
if (self.resultSearchController.active){
let event = filteredTableData[indexPath.row]
}else{
let event = events[indexPath.row]
}
let imageArray = [event.image]
for eventImage in imageArray {
if eventImage != nil {
return true
}
}
return false
}
如标题所示,我的 UISearchController 在搜索结果中为第一个单元格显示错误的单元格原型时遇到了问题。
背景信息:我有两个单元格原型,一个没有图像(标识符:basicCell),另一个带有 UIImageView(标识符:imageCell)。单元格在不搜索时工作正常。
问题的详细描述: 当我单击搜索栏时一切正常,直到我开始搜索某些东西。当我这样做时,第一个单元格总是有 imageCell 标识符(显示一个灰色的空图像视图,表示缺少图像),无论如何。注意:在搜索任何内容之前,tableview 中的第一个单元格有一个自定义图像……也许这是值得注意的? 无论如何,我不知道我做错了什么。有人介意帮忙吗?
代码:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if (self.resultSearchController.active) {
if hasImageAtIndexPath(indexPath) {
let cell = tableView.dequeueReusableCellWithIdentifier(imageCellIdentifier, forIndexPath: indexPath) as! TimelineTableViewCellImage
let event = filteredTableData[indexPath.row]
cell.content.text = profile.content
cell.name.text = profile.name
//This is the image
cell.attachment.image = profile.image
cell.attachment.layer.cornerRadius = 1
cell.attachment.clipsToBounds = true
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier(basicCellIdentifier, forIndexPath: indexPath) as! TimelineTableViewCell
let event = filteredTableData[indexPath.row]
cell.content.text = profile.content
cell.name.text = profile.name
return cell
}
} else {
if hasImageAtIndexPath(indexPath) {
let cell = tableView.dequeueReusableCellWithIdentifier(imageCellIdentifier, forIndexPath: indexPath) as! TimelineTableViewCellImage
let event = events[indexPath.row]
cell.content.text = profile.content
cell.name.text = profile.name
cell.attachement.image = profile.image
cell.attachment.layer.cornerRadius = 1
cell.attachment.clipsToBounds = true
return cell
} else {
let cell = tableView.dequeueReusableCellWithIdentifier(basicCellIdentifier, forIndexPath: indexPath) as! TimelineTableViewCell
let event = events[indexPath.row]
cell.content.text = profile.content
cell.name.text = profile.name
return cell
}
}
}
这是我检查图像的代码:
func hasImageAtIndexPath(indexPath:NSIndexPath) -> Bool {
let event = events[indexPath.row]
let imageArray = [event.image]
for eventImage in imageArray {
if eventImage != nil {
return true
}
}
return false
}
您的 hasImageAtIndexPath:
函数中需要有一个 if-else 子句,就像您在 cellForRowAtIndexPath:
中一样。如果 table 视图是搜索 table,则 event
需要按照与 cellForRowAtIndexPath:
、
func hasImageAtIndexPath(indexPath:NSIndexPath sender:UITableView) -> Bool
if (self.resultSearchController.active){
let event = filteredTableData[indexPath.row]
}else{
let event = events[indexPath.row]
}
let imageArray = [event.image]
for eventImage in imageArray {
if eventImage != nil {
return true
}
}
return false
}