2010-04-07 98 views
2

我新的目標C和iPhone編程。我似乎無法弄清楚爲什麼當我運行此代碼時沒有單元格填充。 xml內容顯示在控制檯日誌中,xcode不顯示任何錯誤。有任何幫助嗎?的UITableView:NSMutableArray裏沒有出現在表

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict 
{ 
    if(![elementName compare:@"Goal"]) { 
     tempElement = [[xmlGoal alloc] init]; 
    } else if (![elementName compare:@"Title"]) { 
     currentAttribute = [NSMutableString string]; 
    } else if (![elementName compare:@"Progress"]) { 
     currentAttribute = [NSMutableString string]; 
    } 
} 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{ 
    if (![elementName compare:@"Goal"]) { 
     [xmlElementObjects addObject:tempElement]; 
    } else if (![elementName compare:@"Title"]) { 
     NSLog(@"The Title of this event is %@", currentAttribute); 
     [tempElement setTitled:currentAttribute]; 
    } else if (![elementName compare:@"Progress"]) { 
     NSLog(@"The Progress of this event is %@", currentAttribute); 
     [tempElement setProgressed:currentAttribute]; 
    } 
} 

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
{ 
    if (self.currentAttribute) { 
     [self.currentAttribute appendString:string]; 
    } 
} 

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [mtableview dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    // Set up the cell... 
    cell.textLabel.text = [xmlElementObjects objectAtIndex:indexPath.row]; 
    return cell; 
} 

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

} 

回答

1

我似乎無法找出爲什麼當我運行這段代碼沒有 細胞將填補。

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

是這種方法擊中?如果是這樣,是xmlElementObjectsnil?如果不是,它是否返回0,因爲數組中沒有元素?如果是這樣,是因爲在使用數組之前,您的XML解析代碼沒有填充數組?

0

你需要檢查的另一件事:你有沒有你的tableview的DataSource屬性設置爲正確的對象?除此之外,請檢查您if(![elementName compare:@"Goal"])真的(通過的NSLog()ING或將一個斷點「重災區」。

另外要注意的,和不相關的,你應該將它添加到xmlElements字典後釋放tempElement

0

-tableView:numberOfRowsInSection:-tableView:cellForRowAtIndexPath:設置斷點,以確保他們被稱爲阿爾方提到的,這可能是因爲你的表視圖的datasource屬性沒有被設置

一些不相關的注意事項:。

  1. 你爲什麼要使用語句![someString compare:someOtherString]作比較? NSString有一個內置的-isEqualToString:方法,這樣你就可以[someString isEqaulToString:someOtherString]替換聲明。你的結果不會改變,但你的代碼將受益於增加的可讀性。

  2. -tableView:cellForRowAtIndexPath:,你有下面的代碼:

    UITableViewCell *cell = [mtableview 
              dequeueReusableCellWithIdentifier:CellIdentifier]; 
    

    ,如果你繼續使用實例變量(在這種情況下,mtableview)這兒,這可能會導致問題。該方法有一個表視圖作爲參數,因此改爲使用tableView。這將確保您從調用此方法的表視圖中取出一個單元格。

  3. 你開發iPhone OS 3.0或以上?如果是這樣,行

    [[UITableViewCell alloc] initWithFrame:CGRectZero 
             reuseIdentifier:CellIdentifier]; 
    

    應該

    [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:CellIdentifier]; 
    

    使用你想要哪個單元格樣式,自然地進行更換。

-1

Objective C對我來說是非常新的。我習慣於編寫PHP,與目前相比,這似乎很容易。我設置數據源,並改變比較isequaltostring。這是控制檯日誌現在狀態:

2010-04-07二點28分16秒。580桶列表[6449:20B]該事件的標題是 試驗1 10%

2010-04-07 02:28:16.581桶列表[6449:20B]該事件的標題是 試驗2 20%

2010-04-07 02:28:16.584列表[6449:20B] * - [xmlGoal isEqualToString:]:無法識別的選擇發送到實例0x3d36600 2010-04-07 02:28:16.585列表[6449:20b] *由於未捕獲的異常'NSInvalidArgumentException'而終止應用程序,原因:'*** - [xmlGoal isEqualToString:]:無法識別的選擇器發送到實例0x3d36600'

我感謝所有的幫助!

+0

不要一個答案回答自己的問題。改爲編輯原始帖子。 – 2010-04-08 04:25:26

0

那麼xmlElementObjects不是一個字符串數組,它包含您創建的一些自定義類的實例(我假設)。因此,當您說cell.textLabel.text = [xmlElementObjects objectAtIndex:indexPath.row];時,您正在爲某個未知對象設置NSString

嘗試cell.textLabel.text = [[xmlElementObjects objectAtIndex:indexPath.row] titled];

(我是從解析代碼猜測標題名稱)