2009-12-23 94 views
1

我想在viewDidLoad中使用appRecord.myName,
當我把它放在那裏它錯誤了,該appRecord是未申報的,如何聲明它?Objective-C語法初學者問題

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

    static NSString *CellIdentifier = @"pilotWay"; 
    static NSString *PlaceholderCellIdentifier = @"PlaceholderCell"; 

    int nodeCount = [self.entries count]; 

    if (nodeCount == 0 && indexPath.row == 0) 
    { 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:PlaceholderCellIdentifier]; 
     if (cell == nil) 
     { 
      cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
              reuseIdentifier:PlaceholderCellIdentifier] autorelease]; 
      cell.detailTextLabel.textAlignment = UITextAlignmentCenter; 
      cell.selectionStyle = UITableViewCellSelectionStyleNone; 
     } 

     cell.detailTextLabel.text = @"load"; 

     return cell; 
    } 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle 
             reuseIdentifier:CellIdentifier] autorelease]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    } 

    if (nodeCount > 0) 
    { 
     AppRecord *appRecord = [self.entries objectAtIndex:indexPath.row]; 


     cell.textLabel.text = appRecord.myName;    
     cell.textLabel.text = appRecord.appName; 
    } 

    return cell; 
} 
+0

我很困惑你想做什麼。它看起來像你的表中的每一行都有一個唯一的AppRecord對象。如果是這樣,在viewDidLoad方法中聲明一個AppRecord有什麼意義?當您的viewController類被加載時,該方法將被調用一次,而cellForRowAtIndexPath會在您的單元格被加載時被調用多次。 – kubi 2009-12-23 01:50:43

+0

我們不要將Objective-C問題標記爲C.我們對C和C++有足夠的疑惑,因爲它沒有使用該標記拋出第三種語言。 – 2009-12-23 01:50:46

+0

我只想在另一個從viewDidLoad調用的方法中使用myName的值(一個名爲AppRecord的不同類中聲明的NSString對象),如何? – 2009-12-23 02:12:35

回答

-1

看來,appRecord聲明(=這是否)。也許你需要投它,但:(AppRecord *)[self.entries objectAtIndex:indexPath.row]。不管你做什麼,要檢查appRecord是否存在,請嘗試以下和運行:

NSLog(@"appRecord: %@", appRecord); 

你應該得到的對象appRecord的描述與一個內存地址,如果它存在。讓我知道這是否有幫助,或者如果你需要進一步的解釋。

+0

這個類沒有在任何地方聲明,我想象的是他的問題。 – 2009-12-23 01:17:21

+0

感謝您的迴應! appRecord在未從viewDidLoad調用的方法中聲明,所以appRecord仍未定義。 – 2009-12-23 01:23:28

+0

該類聲明,在類中一切正常,但如果我想在viewDidLoad appRecord.myName用於另一個目的appRecord尚未定義,如何定義它? – 2009-12-23 01:29:33

1

您有#import "AppRecord.h"頂級文件嗎?

+0

是的,在類中一切正常,但如果我想在viewDidLoad中使用appRecord.myName作爲另一個目的,appRecord尚未定義,如何定義它? – 2009-12-23 01:26:33