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;
}
编辑:
决定整体使用标签方法充满了问题。通过使用这篇文章中的解决方案来解决问题检测在 UITableView 中按下了哪个 UIButton