Iphone 滚动UITableView以显示底部行时,应用程序崩溃
我有一个在UITableView中显示数据的应用程序。然而,当我试图显示底部的行时,它崩溃了。希望有人能解释为什么会发生这种情况Iphone 滚动UITableView以显示底部行时,应用程序崩溃,iphone,objective-c,uitableview,exc-bad-access,Iphone,Objective C,Uitableview,Exc Bad Access,我有一个在UITableView中显示数据的应用程序。然而,当我试图显示底部的行时,它崩溃了。希望有人能解释为什么会发生这种情况 -(void)viewDidLoad { NSLog(@"myString is :%@", myString); int processID = [myString intValue]; //NSLog(@"transferredArray is %@", transferredArray); task = [[NSTask al
-(void)viewDidLoad {
NSLog(@"myString is :%@", myString);
int processID = [myString intValue];
//NSLog(@"transferredArray is %@", transferredArray);
task = [[NSTask alloc] init];
[task setLaunchPath: @"/bin/ps"];
arguments = [NSArray arrayWithObjects: @"aux", [NSString stringWithFormat:@"%i", processID],nil];
[task setArguments: arguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput:pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[task launch];
NSData *data;
data = [file readDataToEndOfFile];
NSString *string;
string = [[NSString alloc] initWithData: data
encoding: NSUTF8StringEncoding];
NSLog(@"ps aux output :%@",string);
NSArray *lines= [string componentsSeparatedByString:@"\n"];
NSString *lastline = [lines objectAtIndex:[lines count]-2];
// NSLog(@"%@",lastline);
lines2= [lastline componentsSeparatedByString:@" "];
NSLog(@"%@",lines2);
for (int i=0; i<[lines2 count]; i++) {
if([[lines2 objectAtIndex:i] isEqualToString:@""]){
[lines2 removeObjectAtIndex:i];
}
}
for (int i=0; i<[lines2 count]; i++) {
if([[lines2 objectAtIndex:i] isEqualToString:@""]){
[lines2 removeObjectAtIndex:i];
}
}
NSLog(@"lines2 after for loops is %@", lines2);
NSLog(@"Lines 2 is%@",lines2);
// NSLog(@"Status is %@",[lines2 objectAtIndex:7]);
self.title = @"Process Info";
label = [[NSMutableArray alloc]init];
[label addObject:@"User:"];
[label addObject:@"Process ID:"];
[label addObject:@"CPU(%):"];
[label addObject:@"MEM(%):"];
[label addObject:@"VSZ"];
[label addObject:@"RSS"];
[label addObject:@"TT"];
[label addObject:@"STAT:"];
[label addObject:@"Time Started:"];
[label addObject:@"Time Elapsed:"];
[label addObject:@"Command:"];
[super viewDidLoad];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel *label2;
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
cell.textColor = [UIColor colorWithRed:154.0/255.0 green:14.0/255.0 blue:2.0/255.0 alpha:1];
cell.font = [UIFont systemFontOfSize:16.0];
label2 = [[[UILabel alloc] initWithFrame:CGRectMake(120.0, 0, 240.0,
tableView.rowHeight)]autorelease];
label2.font = [UIFont systemFontOfSize:16.0];
NSString *cellValue1 = [lines2 objectAtIndex:indexPath.row];
label2.text = cellValue1;
label2.textAlignment = UITextAlignmentLeft;
label2.textColor = [UIColor blackColor];
label2.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label2];
}
NSString *cellValue = [label objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
}
- (void)viewWillDisappear:(BOOL)animated
{
[task terminate];
[task release];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [lines2 count];
}
- (void)dealloc
{
//[transferredArray release];
//[myString release];
//[arguments release];
//[ResultStringID release];
//[values release];
//[label release];
[super dealloc];
}
-(void)viewDidLoad{
NSLog(@“myString是:%@”,myString);
int processID=[myString intValue];
//NSLog(@“transferredArray为%@”,transferredArray);
任务=[[NSTask alloc]init];
[任务集启动路径:@/bin/ps];
参数=[NSArray arrayWithObjects:@“aux”,[NSString stringWithFormat:@“%i”,processID],nil];
[任务集参数:参数];
NSPipe*管道;
管道=[NSPipe pipe];
[任务设置标准输出:管道];
NSFileHandle*文件;
文件=[pipe fileHandleForReading];
[任务启动];
NSData*数据;
数据=[file readDataToEndOfFile];
NSString*字符串;
string=[[NSString alloc]initWithData:data
编码:NSUTF8StringEncoding];
NSLog(@“ps辅助输出:%@”,字符串);
NSArray*行=[string componentsSeparatedByString:@“\n”];
NSString*lastline=[lines objectAtIndex:[lines count]-2];
//NSLog(@“%@”,最后一行);
lines2=[lastline组件由字符串分隔:@”“];
NSLog(@“%@”,第2行);
对于(int i=0;i将[lines2 retain]作为viewDidLoad方法中的最后一条语句,它将很好地工作。想到两件事:
- 您正试图从
NSArray
(第2行)中删除对象。这是不可能的
- 您的表视图根据
lines2
的计数确定其行数,但您尝试使用indexPath.row访问标签
数组-如果lines2
中的条目多于标签
,这将导致崩溃
- 您永远不会更改
标签2
标签中的值,这在滚动时看起来很奇怪。您应该给它一个标签,然后在设置cell.textLabel.text的同一位置更改内容
lines2
是自动删除的,因此当您在cellforrowatinexpath
在方法的numberOfRowsInSection中为您提供了[lines2 count],但您正在cellForRowAtIndexPath中使用标签数组。如果标签的计数小于lines2 count,它将崩溃