亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

這個錯誤個人認為是對一個已經釋放的對象進行操作所產生的,但是不知道錯誤發生在什么地方啊!

做的是一個UITableView單選的簡單Demo,但是在行選擇的時候出現了EXC_BAD_ACCESS錯誤先上代碼:UITableViewSingleChoiceViewController.h@interface UITableViewSingleChoiceViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>@property(nonatomic,retain)NSIndexPath *oldIndex;@endUITableViewSingleChoiceViewController.m#import "UITableViewSingleChoiceViewController.h"#import "Cell.h"  //自定義的一個UITableViewCell@interface UITableViewSingleChoiceViewController ()@end@implementation UITableViewSingleChoiceViewController{    UITableView *table; }@synthesize oldIndex; -(void)viewDidLoad {     [super viewDidLoad];     table=[[UITableView alloc]initWithFrame:self.view.bounds];     table.delegate=self;     table.dataSource=self;     [self.view addSubview:table]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    return 10; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"Cell" owner:self options:nil];     Cell *cell=[nib objectAtIndex:0];    if(indexPath.row==2){      cell.thumbnail.image=[UIImage imageNamed:@"帳號切換"];         oldIndex=indexPath;     }else{         cell.thumbnail.image=[UIImage imageNamed:@"帳號切換2"];     }     cell.label.text=[NSString stringWithFormat:@"tableview cell - %d",indexPath.row+1];    return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {    if([indexPath isEqual:oldIndex]){        NSLog(@"行選擇沒有變");     }else{         Cell *newCell = (Cell *)[tableView cellForRowAtIndexPath:indexPath];         newCell.thumbnail.image=[UIImage imageNamed:@"帳號切換"];         Cell *oldCell = (Cell *)[tableView cellForRowAtIndexPath:oldIndex];         oldCell.thumbnail.image=[UIImage imageNamed:@"帳號切換2"];         oldIndex=indexPath;     } }@end在 tableView didSelectRowAtIndexPath方法中的第一行就出現了EXC_BAD_ACCESS錯誤信息
查看完整描述

2 回答

?
函數式編程

TA貢獻1807條經驗 獲得超9個贊

看起來是你的oldIndex出了問題。兩個建議

1,用copy來管理oldIndex,并且在調用時使用 self.

@property (nonatomic, copy) NSIndexPath *oldIndex;
NSLog(@"section: %d, row: %d", self.oldIndex.section, self.oldIndex.row);

2,不直接比較NSIndexPath對象

if (indexPath.section == self.oldIndex.section
      && indexPath.row == self.oldIndex.row) {    ;;}


查看完整回答
反對 回復 2023-04-22
?
子衿沉夜

TA貢獻1828條經驗 獲得超3個贊

oldIndex第一次使用的時候,還光聲明了,沒初始化吧,假如默認沒走“indexPath.row==2”分支的話

查看完整回答
反對 回復 2023-04-22
  • 2 回答
  • 0 關注
  • 204 瀏覽

添加回答

了解更多

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號