![](/img/trans.png)
[英]Populate UITableView Sections table with Single NSMutableArray
[英]UITableView: NSMutableArray not appearing in table
我是目標c和iPhone編程的新手。 我似乎無法弄清楚為什么在運行此代碼時沒有單元格會填滿。 xml內容顯示在控制台日志中,而xcode不顯示任何錯誤。 可以幫忙嗎?
- (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 {
}
我似乎無法弄清楚為什么在運行此代碼時沒有單元格會填滿。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [xmlElementObjects count];
}
這種方法會受到打擊嗎? 如果是這樣, xmlElementObjects
nil
? 如果不是,因為數組中沒有元素,它是否返回0? 如果是這樣,是因為您的XML解析代碼沒有在使用數組之前填充數組嗎?
您需要檢查的另一件事:是否將表視圖的dataSource屬性設置為正確的對象? 除此之外,檢查您的if(![elementName compare:@"Goal"])
是否真的“命中”(通過NSLog()ing或在此處放置斷點)。
另一個注意事項,與此無關,您可能應該在將tempElement
添加到xmlElements
字典后釋放它。
在-tableView:numberOfRowsInSection:
和-tableView:cellForRowAtIndexPath:
設置斷點,以確保它們被調用。 如Alfons所述,可能是您的表格視圖的datasource
屬性未設置。
一些不相關的注釋:
為什么要使用語句![someString compare:someOtherString]
進行比較? NSString
具有內置的-isEqualToString:
方法,因此您可以將該語句替換為[someString isEqaulToString:someOtherString]
。 您的結果不會改變,但是您的代碼將受益於增加的可讀性。
在-tableView:cellForRowAtIndexPath:
,您具有以下代碼:
UITableViewCell *cell = [mtableview dequeueReusableCellWithIdentifier:CellIdentifier];
如果您在此處繼續使用實例變量(在本例中為mtableview
),則可能會導致問題。 該方法將表視圖作為參數,因此請改用tableView
。 這將確保您從調用此方法的表視圖中將單元出隊。
您是否正在開發iPhone OS 3.0或更高版本? 如果是這樣,那行
[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
應該替換為
[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
自然使用您想要的任何單元格樣式。
xmlElementObjects
不是字符串數組,它包含您創建的某些自定義類的實例(我假設)。 因此,當您說cell.textLabel.text = [xmlElementObjects objectAtIndex:indexPath.row];
,您正在將NSString
設置為某些未知對象。
嘗試cell.textLabel.text = [[xmlElementObjects objectAtIndex:indexPath.row] titled];
(我從解析代碼中猜測標題名稱)
目標C對我來說是非常新的。 到目前為止,我已經習慣寫PHP了,與之相比,這似乎非常容易。 我設置了數據源,還更改了與isequaltostring的比較。 這是控制台日志現在指出的內容:
2010-04-07 02:28:16.580遺願清單[6449:20b]該活動的標題是Test 1 10%
2010-04-07 02:28:16.581遺願清單[6449:20b]該活動的標題是Test 2 20%
2010-04-07 02:28:16.584 list [6449:20b] *-[xmlGoal isEqualToString:]:無法識別的選擇器已發送至實例0x3d36600 2010-04-07 02:28:16.585 list [6449:20b] *正在終止應用程序到未捕獲的異常“ NSInvalidArgumentException”,原因:“ ***-[xmlGoal isEqualToString:]:無法識別的選擇器已發送到實例0x3d36600”
我感謝所有的幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.