2011-09-08 10 views
0

我的UITableView正常顯示,沒有問題。但是,每當用戶嘗試向下滾動以顯示底部行時,它都會與EXEC_BAD_ACCESS一起崩潰。我已經嘗試了每個[someobject release]評論,但無濟於事。我懷疑罪魁禍首是cellForRowAtIndexPath方法。這裏是我的整個控制器的代碼:當用戶試圖滾動顯示較低的行時,UITableView會拋出EXEC_BAD_ACCESS

-(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]; 
     } 

    } 

    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:@"0"]){ 
      [lines2 removeObjectAtIndex:i]; 
     } 

    } 

    for (int i=0; i<[lines2 count]; i++) { 

     if([[lines2 objectAtIndex:i] isEqualToString:@"0.0"]){ 
      [lines2 removeObjectAtIndex:i]; 
     } 

    } 

    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:@"0:00.00"]){ 
      [lines2 removeObjectAtIndex:i]; 
     } 

    } 

    NSLog(@"lines2 after for loops is %@", lines2); 









    NSLog(@"Lines 2 is%@",lines2); 



    self.title = @"Process Info"; 

    label = [[NSMutableArray alloc]init]; 

    [label addObject:@"User:"]; 
    [label addObject:@"Process ID:"]; 
    [label addObject:@"CPU(%):"]; 
    [label addObject:@"MEM(%):"]; 
    [label addObject:@"Status:"]; 
    [label addObject:@"Time Started:"]; 
    [label addObject:@"Launch Path:"]; 
    [label addObject:@"STAT:"]; 
    [label addObject:@"Time Started:"]; 
    [label addObject:@"Time Elapsed:"]; 
    [label addObject:@"Launch Command:"]; 

    [super viewDidLoad]; 
} 


/* 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return YES; 
} 
*/ 


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 


    /* 
    NSString *CellIdentifier = [NSString stringWithFromat:@"Cell_%d_%d",indexPath.section,indexPath.row]; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    */ 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease]; 
    } 

    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
    NSString *cellValue = [label objectAtIndex:indexPath.row]; 
    cell.textLabel.text = cellValue; 
    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]; 


    UILabel *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]; 


    return cell; 
} 


-(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]; 
} 

任何幫助非常感謝! :)

回答

1

我認爲這將有助於:

-(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; 
} 

,因爲你的TableCell是dequeueble所以你shoulden't初始化標籤如果小區不爲零。只更改tableview更新時的標籤文本(或者如果cell爲零,則初始化所有內容)

+0

感謝兄弟!用於粘貼整個cellForRowAtIndexPath方法的+1) –

相關問題