1
我有一個簡單的UITableView,我想使用plist文件填充。我遵循了幾個教程,但它們似乎都讓這個過程複雜化了,最終我得到了錯誤,或者我的應用程序根本無法工作。如何使用plist填充UITableView
我正在使用基於窗口的應用程序。
乾杯, 山姆
我有一個簡單的UITableView,我想使用plist文件填充。我遵循了幾個教程,但它們似乎都讓這個過程複雜化了,最終我得到了錯誤,或者我的應用程序根本無法工作。如何使用plist填充UITableView
我正在使用基於窗口的應用程序。
乾杯, 山姆
要加載文件:
thearray = [NSArray arrayWithContentsOfFile:thePath];
然後,你需要設置你的控制器作爲數據源,並委託表視圖,並實現至少:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [thearray count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell* cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
// assuming your array contains only simple strings :
cell.textLabel.text = [thearray objectAtIndex:indexPath.row];
return cell;
}
這不使用單元格的「重用」,如果列表很大,這很重要。
感謝您的回覆,有沒有辦法選擇我想返回的字段。我有一個'root',它是一個數組。在裏面我有'item 0'這是一本字典。在那本字典裏面我有名字,位置等等。我怎樣才能從每個項目返回'name'? – sam
在cellForRow ...方法中,您可以編寫cell.textLabel.text = [[thearray objectAtIndex:indexPath.row] objectForKey:@「name」]; – gregory
還記得要做[self.tableView reload];加載數組後 –