Application crashes when scrolling UITableView to reveal bottom rows
I have an application which displays data in a UITableView. However, it crashes when i try to reveal the bottom rows. Hope someone can shed some light on why this is happening.
-(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 o开发者_开发问答bjectAtIndex: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];
}
Note : I commented out all the [release]'s in the dealloc method to prevent any other EXEC_BAD_ACCESS errors, although the current error I am encountering is EXEC_BAD_ACCESS
Couple of things spring to mind:
- You are trying to remove objects from an
NSArray
(lines2). This is not possible. - Your table view determines its number of rows from the count of
lines2
, but you attempt to access thelabels
array with the indexPath.row - if there are more entries inlines2
thanlabels
this will cause a crash. - You never change the value in your
label2
label which will look very odd when scrolling. You should give this a tag and then change the content in the same place as you setcell.textLabel.text
lines2
is autoreleased so may not be around when you come to access it incellForRowAtIndexPath
put [lines2 retain] as last statement in viewDidLoad method it will work fine.
精彩评论