[英]Application crashes when scrolling UITableView to reveal bottom rows
我有一個在UITableView中顯示數據的應用程序。 但是,當我嘗試顯示最下面的行時,它會崩潰。 希望有人可以闡明為什么會這樣。
-(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];
}
注意:盡管我遇到的當前錯誤是EXEC_BAD_ACCESS,但我注釋掉了dealloc方法中的所有[release]以防止發生任何其他EXEC_BAD_ACCESS錯誤
我想到了幾件事:
NSArray
刪除對象(第2行)。 這是不可能的。 lines2
,但您試圖訪問labels
與indexPath.row陣列-是否有更多的條目lines2
比labels
,這將導致崩潰。 label2
標簽中的值,當滾動時,該值看起來很奇怪。 您應該給它一個標簽,然后在設置cell.textLabel.text
位置更改內容。 lines2
是自動釋放的,因此當您在cellForRowAtIndexPath
訪問它時可能不會出現 將[lines2 keep]作為viewDidLoad方法中的最后一條語句,它將可以正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.