尝试编辑单元格时UITableView崩溃

时间:2009-12-10 08:56:03

标签: iphone uitableview crash scroll

我正在使用表格视图中的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错误。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

另一个问题......以下是我从页面获取数据的方法

-(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 = @"";
}

当我按下页面上的SignUp按钮时,我只看到网站和电子邮件字段。当我记录变量时,只有电子邮件和网站在那里,其他的是@“”。发生了什么事?

答案 1 :(得分:0)

当FreeSignupCell消失时(比如因为它从顶部或底部滚动,或被重用),来自该被破坏的单元格的getText消失并获得[文本释放];对你来说,你自己会发生什么.firstName,self.lastName,self.company,self.website和self.email现在引用一个free()'d?

的字符串对象

我认为你应该使用[[cell getText] retain];代替。