2011-07-27 7 views
0

現在、iPhoneはUITableView経由ですべてのバックグラウンドプロセスを一覧表示するアプリケーションをビルドしています。ただし、最後のprocessIDとprocessNameのみが正常にリストされています。すべての希望の情報を取得できません

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
return [arryData count]; 
} 

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
return @"Process ID    Process Name"; 
} 



// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
} 


// Set up the cell... 
NSString *cellValue= [arryData objectAtIndex:indexPath.row]; 

cell.text = cellValue; 
return cell; 
} 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
} 
@end 

すべてのヘルプは非常に高く評価されるだろう:

プロセス情報を取得するためのメソッドをviewDidLoadメソッドに保存されており、この

- (void)viewDidLoad { 


int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0}; 
size_t miblen = 4; 

size_t size; 
int st = sysctl(mib, miblen, NULL, &size, NULL, 0); 

struct kinfo_proc * process = NULL; 
struct kinfo_proc * newprocess = NULL; 

do { 

    size += size/10; 
    newprocess = realloc(process, size); 

    if (!newprocess){ 

     if (process){ 
      free(process); 
     } 


    } 

    process = newprocess; 
    st = sysctl(mib, miblen, process, &size, NULL, 0); 

} while (st == -1 && errno == ENOMEM); 

if (st == 0){ 

    if (size % sizeof(struct kinfo_proc) == 0){ 
     int nprocess = size/sizeof(struct kinfo_proc); 

     if (nprocess){ 

      NSMutableArray * array = [[NSMutableArray alloc] init]; 

      for (int i = nprocess - 1; i >= 0; i--){ 


       NSString * processID = [[NSString alloc] initWithFormat:@"%d", process[i].kp_proc.p_pid]; 
       NSString * processName = [[NSString alloc] initWithFormat:@"%s", process[i].kp_proc.p_comm]; 
       NSString * combinedString = [NSString stringWithFormat:@"%@ %@ %@", processID, @"   " ,processName]; 

       NSDictionary * dict = [[NSDictionary alloc] initWithObjects:[NSArray arrayWithObjects:processID, processName, nil] 
                    forKeys:[NSArray arrayWithObjects:@"ProcessID", @"ProcessName", nil]]; 




        arryData = [[NSArray alloc] initWithObjects:[[NSMutableString alloc]initWithFormat:@"%@", combinedString], nil]; 





       //For debugging purposes 
       NSLog(@"%@", dict); 


       [processID release]; 
       [processName release]; 
       [array addObject:dict]; 
       [dict release]; 
      } 

      free(process); 
      return [array autorelease]; 
     } 
    } 
} 



[super viewDidLoad]; 
} 

のように見えている私のテーブルの方法は次のようになります! :)

答えて

0

arryDataには、forループの繰り返しごとに新しい配列を割り当てます。 arryDataに複数の要素を含めることはできません。また、おそらくそれらの配列の最後を除いてすべてを漏れているでしょう。

+0

どうすればいいですか?申し訳ありませんが、Objective Cの新機能です。 –

+0

forループの前にNSMutableArrayを作成し、1つの要素を含む配列を1つの他の要素を含む新しい配列に置き換えるのではなく、各繰り返しで項目を追加します。 – Jonah

関連する問題