Ios 如何在选中行时添加图像,然后在SWIFT的UITableView中取消选中时删除图像
我遇到了一个问题,当选择一行时使图像出现,然后当选择另一行时使图像消失。如果我触摸选定的行,它不会被取消选择–这是可以的,因为这是我想要的行为。我只希望在触摸另一行时取消选择当前选定的行 我正在用Swift写作 我正在使用解决方案展开所选行,如回答的那样 此UIImage应出现/消失:cellContent.ringImage.image=UIImage(名为:“ring.png”) 我猜我的逻辑在下面的selectedCellIndexPath部分是错误的 这是我的代码: 在我的TVC中:Ios 如何在选中行时添加图像,然后在SWIFT的UITableView中取消选中时删除图像,ios,swift,uitableview,ios8,uiimage,Ios,Swift,Uitableview,Ios8,Uiimage,我遇到了一个问题,当选择一行时使图像出现,然后当选择另一行时使图像消失。如果我触摸选定的行,它不会被取消选择–这是可以的,因为这是我想要的行为。我只希望在触摸另一行时取消选择当前选定的行 我正在用Swift写作 我正在使用解决方案展开所选行,如回答的那样 此UIImage应出现/消失:cellContent.ringImage.image=UIImage(名为:“ring.png”) 我猜我的逻辑在下面的selectedCellIndexPath部分是错误的 这是我的代码: 在我的TVC中: c
class MenuViewController: UIViewController{
var selectedCellIndexPath: NSIndexPath?
let SelectedCellHeight: CGFloat = 222.0
let UnselectedCellHeight: CGFloat = 64.0
let menuItems = [
("1","test 1"),
("2","test 2"),
("3","test 3"),
("4","test 4"),
("5","test 5")]
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == menuTable {
return menuItems.count
} else {
return 0}
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MenuTableViewCell
if tableView == menuTable {
let (my, section) = menuItems[indexPath.row]
cell.myLabel.text = my
cell.sectionLabel.text = section
cell.selected = true
}
}
func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
if let selectedCellIndexPath = selectedCellIndexPath {
if selectedCellIndexPath == indexPath {
return SelectedCellHeight
}
}
return UnselectedCellHeight
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
var cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! MenuTableViewCell
var cellContent = tableView.cellForRowAtIndexPath(indexPath) as! MenuTableViewCell
let cellLabel = cellContent.sectionLabel.text
if let selectedCellIndexPath = selectedCellIndexPath {
if selectedCellIndexPath != indexPath {
self.selectedCellIndexPath = indexPath
cellContent.ringImage.image = UIImage(named: "ring.png")
} else {
self.selectedCellIndexPath != indexPath
cellContent.ringImage.hidden = true
tableView.deselectRowAtIndexPath(indexPath, animated: true)
// cellContent.testbutton.removeFromSuperView
}
} else {
selectedCellIndexPath = indexPath
cellContent.ringImage.hidden = true
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
tableView.beginUpdates()
tableView.endUpdates()
}
非常感谢你的帮助
谢谢
Mikee似乎
self.selectedCellIndexPath!=indexPath
不会产生所需的取消选择标记效果。您可以尝试使用tableView.indexPathsForSelectedRows()
获取当前选定的indexPath,将其与参数中的indexPath进行比较,然后在不分配self.selectedCellIndexPath
的情况下完成逻辑
(编辑)
我发现您还需要varialbeself.selectedCellIndexPath
来标识高度,您可以尝试将其转换为计数器变量,用于计算当前选定行的选定时间。如果它是奇数,则选择它,而当它是偶数时,您知道您将取消选择它并将计数器变量be重置为零
func tableView(tableView: UITableView!, heightForRowAtIndexPath indexPath: NSIndexPath!) -> CGFloat {
if (indexPath == tableView.indexPathsForSelectedRows()[0] && self.counter % 2 == 1) {
return SelectedCellHeight
}
return UnselectedCellHeight
}
谢谢你的建议。当我将heightForRowAtIndexPath中的SelectedCellIndexPath代码替换为您的代码时:if(indexPath==tableView.IndexPathForSelectedRows()[0]&&self.counter%2==1){我收到一个错误“AnyObject不能转换为NSObject”,当我向下转换为!NSObject时,我收到一个错误“找不到接受所提供参数的“==”的重载”。如果我删除IndExpathForSelectedRows()[0]末尾的[0],它将进行编译,但不会扩展行大小。我还删除了selectedCellIndexPath部分,并将其替换为:If tableView.indExpathForSelectedRows()!=indexPath{cellContent.ringImage.image=UIImage(名为:“ring.png”)}else{cellContent.ringImage.removeFromSuperview()}这提供了与当前相同的效果。那么
(tableView.IndExpathForSelectedRows()作为![NSIndexPaths])[0]
呢,然后比较IndExpathForSelectedRows()是否为单选tableView?tableView.IndExpathForSelectedRows()
将返回一个对象数组,您应该在此处向下转换。