【问题标题】:UITableView is crashing when trying to edit cell尝试编辑单元格时 UITableView 崩溃
【发布时间】:2009-12-10 08:56:03
【问题描述】:

我有一些表格,它由表格视图中的 5 个自定义单元格组成。代码如下:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;}

\- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

\- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSLog(@"in here");
    static NSString *MyIdentifier = @"MyIdentifier";

    FreeSignupCell *cell = (FreeSignupCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil) {
        NSLog(@"shazam");
        cell = [[[FreeSignupCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;    
    }

    switch (indexPath.row) {
        case 0:
            NSLog(@"index 0");
            [cell setLabelContent:@"First Name"];
            //self.firstName = [cell getText];
            break;
        case 1:
            NSLog(@"index 1");
            [cell setLabelContent:@"Last Name"];
            //self.lastName = [cell getText];
            break;
        case 2:
            NSLog(@"index 2");
            [cell setLabelContent:@"Company (optional)"];
            //self.company = [cell getText];
            break;
        case 3:
            NSLog(@"index 3");
            [cell setLabelContent:@"Website (optional)"];
            [cell setText: @"http://"];
            //self.website = [cell getText];
            break;
        case 4:
            NSLog(@"index 4");
            [cell setLabelContent:@"E-mail"];
            //self.email = [cell getText];
            break;
        default:
            [cell setLabelContent:@"E-mail"];
    }

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{  
    return 70.0;
}

问题是,当我编辑第一个字段时,快速滚动到最后一个字段时,应用程序崩溃并出现 objc_mssend 错误。

有什么想法吗?

【问题讨论】:

  • 看起来您的问题一定是在 FreeSignupCell 类中的某个地方...在您发布的代码中看不到任何问题
  • 不做任何编辑就上下滚动会不会报错?您可以将代码发布到 FreeSignupCell 吗?尝试打开 NSZombie 以查看正在释放的内容。
  • 不,就在我尝试编辑的时候。有趣的是它发生在模拟器中,而不是在设备上。我很困惑

标签: iphone uitableview crash scroll


【解决方案1】:

另一个问题...这是我从页面获取数据的方式

-(void) SignupAction {

//Fetching signup data from signup form
NSIndexPath *path = [NSIndexPath indexPathForRow:0 inSection:0];
FreeSignupCell *cell = (FreeSignupCell *)[self.tableView cellForRowAtIndexPath:path];
self.firstName = [cell getText];

path = [NSIndexPath indexPathForRow:1 inSection:0];
cell = (FreeSignupCell *)[self.tableView cellForRowAtIndexPath:path];
self.lastName = [cell getText];

path = [NSIndexPath indexPathForRow:2 inSection:0];
cell = (FreeSignupCell *)[self.tableView cellForRowAtIndexPath:path];
self.company = [cell getText];

path = [NSIndexPath indexPathForRow:3 inSection:0];
cell = (FreeSignupCell *)[self.tableView cellForRowAtIndexPath:path];
self.website = [cell getText];

path = [NSIndexPath indexPathForRow:4 inSection:0];
cell = (FreeSignupCell *)[self.tableView cellForRowAtIndexPath:path];
self.email = [cell getText];

//JSON Dictionaries require parameters that are not null
if (self.firstName == nil) {
    self.firstName = @"";
}
if (self.lastName == nil) {
    self.lastName = @"";
}
if (self.company == nil) {
    self.company = @"";
}
if (self.website == nil) {
    self.website = @"";
}
if (self.email == nil) {
    self.email = @"";
}

当我在页面上按下注册按钮时,我只能看到网站和电子邮件字段。发生的情况是,当我记录变量时,只有电子邮件和网站存在,其他的是 @""。发生了什么?

【讨论】:

    【解决方案2】:

    当 FreeSignupCell 消失时(比如因为它从顶部或底部滚动出来,或者被重用),并且来自该被破坏单元格的 getText 消失并获得 [text release];'d on you,你自己会发生什么.firstName、self.lastName、self.company、self.website 和 self.email 现在引用的是 free()'d 的字符串对象?

    我认为你应该使用 [[cell getText] retain];而是。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多