[英]How to have UITableViewCell reference objects in an array?
我有一個具有多個實例變量( name
, address
, number
等)的對象數組。 我還有一個UITableViewController
其表視圖中填充了每個對象的name
變量。
我還創建了一個DetailViewController
,當使用tableView:didSelectRowAtIndexPath:
選擇對象的指定單元格時,應顯示這些對象保留的其余信息。 問題是,這些單元格僅引用該單元格對象的name
變量。
我應該如何解決這個問題? 我是否需要對單元進行子類化,以便為每個單元提供對整個對象的引用? 還是有更簡單的方法?
這是我當前的tableView:cellForRowAtIndexPath:
方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if(!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
}
cell.textLabel.text = [[listings objectAtIndex:indexPath.row] objectForKey:@"listingName"];
return cell;
}
更新:我只是想到我可以從indexPath
獲取行號,並從數組中獲取指定的對象。 這會更可行嗎?
在tableView:didSelectRowAtIndexPath:
您可以僅使用[listings objectAtIndex:indexPath.row]
來獲取所選對象,並將其傳遞給詳細視圖控制器。
(您永遠不要嘗試將表視圖單元格用作數據源。)
由於您是直接從列表的陣列位置顯示列表,因此一旦按下該行,便會以相同的方式再次將它們取出。
在tableView:didSelectRowAtIndexPath:
只需執行[listings objectAtIndex:indexPath.row]
以獲取有問題的對象並將其發送到DetailViewController
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.