2014-09-04 35 views
1

我有字典的阵列填充的tableView所见下面目标C从阵列获取字典[I]

self.tableCell.usernameLabel.text = [self.loadTableArrayCopy[indexPath.row] valueForKeyPath:@"user.full_name"]; 

如果用户喜欢该行的内容,他们可以按下保存按钮单元格和字典中的某些值保存在Core Data中。

问题:并非字典中的所有值都对应于uitableviewcell中的插座。如果不是这样的话,我可以做这样的事情

savedItem.username = self.tableCell.usernameLabel.text 

但由于一些值,比如缩略图,未在单元格中显示,我不知道,我可以做这样的事情,因为它不是与细胞

savedItem.thumbnail = self.thumbnail 

我的优选的选择是采取indexPath.row,访问字典在阵列[indexPath.row]和通过从词典设置管理对象的属性保存相关联。我怎样才能得到这个字典的数组和局部变量,所以我可以做这样的事情?

savedItem.thumbnail = [myLocalDictionary [email protected]"thumbnail"]; 

回答

0

当用户点击保存按钮,那么你可以在以后得到indexPath.row value.So,

NSDictionary *saveDict = [array objectAtIndex:indexPath.row]; 

这样会得到字典,并可能进一步将其保存到coreData。

如果无法找到按钮indexPath.row值,那么你可以如下操作: -

-(void)buttonTapped:(id)sender 
{ 
    UIButton *senderBtn = (UIButton *)sender; 
    UITableViewCell *cell; 

    if (![Utilities isIPhone]) { 
    if ([UIDevice currentDevice].systemVersion.intValue < 7) 
     cell = (UITableViewCell *)[[[senderBtn superview] superview] superview]; 
    else 
     cell = (UITableViewCell *)[[[[senderBtn superview] superview] superview] superview]; 
    } 
    NSIndexPath *index = [advancedSearchTable indexPathForCell:cell]; 
} 

时节省被窃听,你可以添加上面的纹路,并找出指数(indexPath)。稍后,使用索引从数组中获取字典。

+0

凉,objectAtIndex是我正在寻找的方法。我正在搜索NSDictionary的init方法无济于事。 – noobsmcgoobs 2014-09-04 09:53:14

+0

@noobsmcgoobs您的欢迎! – nikhil84 2014-09-04 09:55:04

1

如果你有单节,你可以做到这一点很容易:

cellForRowAtIndexPath一套这样的:

cell.saveButton.tag = indexPath.row; 

从保存按钮事件做到这一点:

-(void)savePressed:(id)sender{ 
    UIButton *btn = (UIButton *)sender; 
    int row = btn.tag; // This is your selected row. 
    NSDictionary *youDictionary = [array objectAtIndex:row]; 

}