Reputation: 451
I was mading a simple demo with UITableView
:When one cell is selected , show a tick in the cell and the last selected cell would be unticked , when run in simulator,it is good when select one cell for the first time,While if selected another cell,The application would be crushed without any log info, I didn't know what happened. Here is what i did in coding :
Header:
#import <UIKit/UIKit.h>
@interface ViewController2 : UIViewController<UITableViewDataSource,UITableViewDelegate>
{
NSIndexPath * oldIndexPath;
NSArray *list;
}
@property(retain,nonatomic)NSIndexPath *oldIndexPath;
@property(strong,nonatomic)NSArray *list;
@end
Implementation:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
if(oldIndexPath == nil){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}else{
UITableViewCell * oldCell = [tableView cellForRowAtIndexPath:oldIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
}
oldIndexPath = indexPath;
}
Any help is appreciated,Thanks
Upvotes: 0
Views: 140
Reputation: 4789
Try This, Hope it will Work for you.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
if(self.oldIndexPath != nil)
{
UITableViewCell * oldCell = [tableView cellForRowAtIndexPath:self.oldIndexPath];
oldCell.accessoryType = UITableViewCellAccessoryNone;
}
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.oldIndexPath = indexPath;
}
Upvotes: 0
Reputation: 119292
Always access your properties via self.
, don't access the ivar directly. By directly assigning you are bypassing any memory management used in the accessor methods, and when you later use the value, it has been released.
In the current version of Xcode, you don't need to declare ivars to back properties or use synthesize. This prevents you accidentally directly accessing the ivars.
Upvotes: 1