
在视图上调用reloadData可以正常工作,但它效率低下并且带有一系列自己的问题(动画可能性,维护选择状态等).
解决方法 这是我要采取的方法.@property (nonatomic,strong) NSIndexPath *selectedindexPath;
将NSIndexPath类型的属性设置为控制器以存储选择的索引路径.
- (voID)tableVIEw:(UItableVIEw *)tableVIEw dIDSelectRowAtIndexPath:(NSIndexPath *)indexPath { self.selectedindexPath = indexPath; [tableVIEw reloadRowsAtIndexPaths:@[indexPath] withRowAnimation: UItableVIEwRowAnimationNone];}- (UItableVIEwCell *)tableVIEw:(UItableVIEw *)tableVIEw cellForRowAtIndexPath:(NSIndexPath *)indexPath { if ([indexPath compare:self.selectedindexPath] == NSOrderedSame) { // Create your custom cell here and return it. } else { // Should create a normal cell and return it. }} 究竟.另请注意,您可能想要取消选择.这是Swift中的完整代码.根据需要在cellForRowAtIndexPath中使用selectedindexPath.
//选择tableVIEwController
导入UIKit
class SelectingtableVIEwController:UItableVIEwController
{
内部var selectedindexPath:NSIndexPath? =没有
overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() tableVIEw.estimatedRowHeight = 68.0 tableVIEw.rowHeight = UItableVIEwautomaticDimension self.cleaRSSelectionOnVIEwWillAppear = false; }overrIDe func tableVIEw (tableVIEw:UItableVIEw,dIDSelectRowAtIndexPath indexPath:NSIndexPath) { print("dID select....") // in fact,was this very row selected,// and the user is clicking to deselect it... // if you don't want "click a selected row to deselect" // then on't include this clause. if selectedindexPath == indexPath { print("(user clicked on selected to deselect)") selectedindexPath = nil tableVIEw.reloadRowsAtIndexPaths( [indexPath],withRowAnimation:UItableVIEwRowAnimation.None) tableVIEw.deselectRowAtIndexPath(indexPath,animated:false) return } // in fact,was some other row selected?? // user is changing to this row? if so,also deselect that row if selectedindexPath != nil { let pleaseRedrawMe = selectedindexPath! // (note that it will be drawn un-selected // since we're chaging the 'selectedindexPath' global) selectedindexPath = indexPath tableVIEw.reloadRowsAtIndexPaths( [pleaseRedrawMe,indexPath],withRowAnimation:UItableVIEwRowAnimation.None) return; } // no prevIoUs selection. // simply select that new one the user just touched. // note that you can not use Apple's willdeselectRowAtIndexPath // functions ... because they are freaky selectedindexPath = indexPath tableVIEw.reloadRowsAtIndexPaths( [indexPath],withRowAnimation:UItableVIEwRowAnimation.None) }} 总结 以上是内存溢出为你收集整理的ios – UITableView,替换选择时的单元格视图全部内容,希望文章能够帮你解决ios – UITableView,替换选择时的单元格视图所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)