![](/img/trans.png)
[英]extract data from plist & then store permanently on tableview's cell iphone?
[英]Problems displaying array from plist in tableview cell
我想在我的Stage字典中讀取並顯示我的數組到tableview中。 但是,我的應用程序崩潰並給出錯誤消息: *由於未捕獲的異常'NSInvalidArgumentException'而終止應用程序,原因:'-[__ NSCFDictionary objectAtIndex:]:無法識別的選擇器已發送至實例0x4b43ee0'
我可以在Stages字典中顯示數組的行數,但不能在表單元格中顯示名稱。
以下是我的列表和代碼。
根(數組)
Item 0 (Dict) Project Name (String) Stage (Dict) Analysis (Array) Design (Array) Develop (Array) Implement (Array)
在我的viewDiDLoad方法中,我調用了
//course is a nsdictionary which is pass down from the previous view
stages = [course objectForKey:@"Stage"];
表格方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger rows = 0;
if (section == 0){
rows = 1;
}
if (section == 1) {
return [stages count];
}
return rows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = @"StagesCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
}
// Set up the cell
if (indexPath.section == 0) {
if (indexPath.row == 0) {
cell.textLabel.text = @"Project Name";
cell.detailTextLabel.text = [course objectForKey:@"ProjectName"];;
}
}
if (indexPath.section == 1) {
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
NSString *cellvalue = [stages objectAtIndex:indexPath.row];
cell.textLabel.text = cellvalue;
}
return cell;
}
提前致謝(:
在您的plist文件中,項目名稱變量如下所示: Project Name
並在以下行中: cell.detailTextLabel.text = [course objectForKey:@"ProjectName"];;
它看起來像這樣: ProjectName
。 我認為兩者必須相同。 也許這只出現在這里。
出現錯誤的原因是,如您在PList文件中所見,stages變量是一個字典,但是您嘗試將其用作數組。 階段字典包含4個鍵,如我所見:分析,設計,開發,設計(字典中奇怪,相同的鍵???)。 因此,您必須使用- (id)objectForKey:(id)aKey;
方法,首先,使用Stages詞典中的鍵之一,然后在此之后,可以使用objectAtIndex:
方法。
stages
元素是字典,而不是數組。 我猜字典中的每個元素都是數組對象,並且您想在表視圖中顯示鍵。
另外,給出的plist似乎與示例不匹配。 您在標有“ course”的字典中指定Stage
,但是在plist中,它顯示Stages
也許您想嘗試:
stages = [[course objectAtIndex:@"Stages"] allKeys];
NSString *cellvalue = [stages.allValues objectAtIndex:indexPath.row];
注意“ allValues”。 allValues返回字典中所有值的數組,allKeys返回字典中所有鍵的數組。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.