当单元格滚动回视图时,表崩溃

时间:2013-07-04 08:45:04

标签: ios ipad uitableview

iPad:我的表格很好,直到单元格延伸到可见区域(屏幕高度)之外。部分屏幕外的最后一个单元格向下滚动,使其不再可见,然后向上滚动应用程序崩溃?为什么呢?

代码重新用于iPhone屏幕,效果很好(这是一款通用应用)。被这一个难倒了

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"JobCell";

    UITableViewCell *cell;
    NSArray *versionCompatibility = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];

    if ( 6 <= [[versionCompatibility objectAtIndex:0] intValue] )
    {
        // iOS6 is installed
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    }
    else
    {
        // iOS5 is installed
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
    }


    // Configure the cell...
    Job *job = [self.jobs objectAtIndex:indexPath.row];

    NSLog(@"Dealing with cell %d",indexPath.row);

    UIColor *redcol = [UIColor colorWithRed:0.62745098039216 green:0.15294117647059 blue:0.15686274509804 alpha:1.0];

    NSString *jobString = job.jobAddress;
    jobString = [jobString stringByAppendingString:@", "];
    jobString = [jobString stringByAppendingString:job.jobPostcode];

    //cell.textLabel.text = jobString;

    NSLog(@"jobString %@",jobString);

    UILabel *jobLabel = (UILabel *)[cell.contentView viewWithTag:10];
    jobLabel.text = jobString;

    int jobsaved = job.jobSaved;

    if (jobsaved == 1)
    {
        //cell.textLabel.textColor = redcol;
        jobLabel.textColor = redcol;
    }
    else
    {
        //cell.textLabel.textColor = [UIColor blackColor];
        jobLabel.textColor = [UIColor blackColor];
    }

    NSLog(@"date %@",job.jobDate);

    UILabel *dateLabel = (UILabel *)[cell.contentView viewWithTag:11];

    if (! [job.jobDate isEqualToString:@""]) {


        NSDateFormatter *formater = [[NSDateFormatter alloc] init];
        [formater setDateFormat:@"yyyy-MM-dd"];

        NSDate *date2 = [formater dateFromString:job.jobDate];
        [formater setDateFormat:@"d MMM YYYY"];

        NSString *date = [formater stringFromDate:date2];

        //cell.detailTextLabel.text = date;
        dateLabel.text = date;

    }
    else
    {
        //cell.detailTextLabel.text = @"";
        dateLabel.text = @"";
    }

    UIButton *mapBtn = (UIButton *)[cell viewWithTag:12];
    [mapBtn addTarget:self action:@selector(showMap:) forControlEvents:UIControlEventTouchUpInside];
    mapBtn.tag = indexPath.row;

    return cell;
}

错误是

 -[UIButton setText:]: unrecognized selector sent to instance 0x2aba90
2013-07-04 09:32:48.563 ESC GasCert[3175:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton setText:]: unrecognized selector sent to instance 0x2aba90'
*** First throw call stack:
(0x37e208bf 0x3796c1e5 0x37e23acb 0x37e22945 0x37d7d680 0x6e0e3 0x319289cb 0x31927aa9 0x31927233 0x318cbd4b 0x37d7f22b 0x37419381 0x37418f99 0x3741d11b 0x3741ce57 0x374446f1 0x374674c5 0x37467379 0x34b76f93 0x3764e891 0x37de9f43 0x37df4553 0x37df44f5 0x37df3343 0x37d764dd 0x37d763a5 0x37b4dfcd 0x318f6743 0x22f1 0x2278)
terminate called throwing an exception(lldb)

崩溃

0x217a:  blx    0xa7ef8                   ; symbol stub for: NSStringFromClass
0x217e:  mov    r7, r7
0x2180:  blx    0xa7f8c                   ; symbol stub for: objc_retainAutoreleasedReturnValue
0x2184:  movs   r2, #0
0x2186:  movt   r2, #0
0x218a:  ldr    r1, [sp, #8]
0x218c:  str    r0, [sp]
0x218e:  mov    r0, r1
0x2190:  ldr    r1, [sp, #4]
0x2192:  ldr    r3, [sp]
0x2194:  blx    0xa7ed8                   ; symbol stub for: UIApplicationMain
0x2198:  str    r0, [sp, #32]

我试过

·H

@property (nonatomic, copy) NSString *jobString;

的.m

_jobString = job.jobAddress;
_jobString = [_jobString stringByAppendingString:@", "];
_jobString = [_jobString stringByAppendingString:job.jobPostcode];

编辑:

这是iPhone故事板的代码,不会崩溃

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"JobCell";

    UITableViewCell *cell;
    NSArray *versionCompatibility = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];

    if ( 6 <= [[versionCompatibility objectAtIndex:0] intValue] )
    {
        // iOS6 is installed
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
    }
    else
    {
        // iOS5 is installed
        cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
    }

    // Configure the cell...
    Job *job = [self.jobs objectAtIndex:indexPath.row];

    UIColor *redcol = [UIColor colorWithRed:0.62745098039216 green:0.15294117647059 blue:0.15686274509804 alpha:1.0];

    NSString *jobString = job.jobAddress;
    jobString = [jobString stringByAppendingString:@", "];
    jobString = [jobString stringByAppendingString:job.jobPostcode];    

    //cell.textLabel.text = jobString;

    UILabel *jobLabel = (UILabel *)[cell viewWithTag:10];
    jobLabel.text = jobString;

    int jobsaved = job.jobSaved;

    if (jobsaved == 1)
    {
        //cell.textLabel.textColor = redcol;
        jobLabel.textColor = redcol;
    }
    else
    {
        //cell.textLabel.textColor = [UIColor blackColor];
        jobLabel.textColor = [UIColor blackColor];
    }

    NSLog(@"date %@",job.jobDate);

    UILabel *dateLabel = (UILabel *)[cell viewWithTag:11];

    if (! [job.jobDate isEqualToString:@""]) {


        NSDateFormatter *formater = [[NSDateFormatter alloc] init];
        [formater setDateFormat:@"yyyy-MM-dd"];

        NSDate *date2 = [formater dateFromString:job.jobDate];
        [formater setDateFormat:@"d MMM YYYY"];

        NSString *date = [formater stringFromDate:date2];

        //cell.detailTextLabel.text = date;
        dateLabel.text = date;

    }
    else
    {
        //cell.detailTextLabel.text = @"";
        dateLabel.text = @"";
    }

    UIButton *mapBtn = (UIButton *)[cell viewWithTag:12];
    [mapBtn addTarget:self action:@selector(showMap:) forControlEvents:UIControlEventTouchUpInside];
    mapBtn.tag = indexPath.row;

    return cell;
}

编辑:

决定整体使用标签方法充满了问题。通过使用此帖Detecting which UIButton was pressed in a UITableView

中的解决方案解决了问题

2 个答案:

答案 0 :(得分:8)

我认为你弄乱了标签,这就是我所看到的:mapBtn.tag = indexPath.row;你不应该像那样重写tag,因为UIButton可能被{viewWithTag检索到1}}而不是UILabel,这就是你获得[UIButton setText:].

的原因

所以你应该找到另一种标记动作的方法。

答案 1 :(得分:-2)

对于iOS 5,您使用

cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

请注意,这将返回nil,因为您尚未初始化标识为CellIdentifier的任何单元格。虽然dequeueReusableCellWithIdentifier:forIndexPath:始终返回一个有效的单元格,但对于dequeueReusableCellWithIdentifier:,您需要初始化单元格(如果没有任何内容可以出列)。

所以你将nil返回- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath(在版本&lt; iOS6上),这会导致我记忆中的崩溃。

以下是documentation段落:

  

返回值

     

具有关联标识符的UITableViewCell对象,如果不是,则为nil   此类对象存在于可重用单元队列中。