2013-06-22 43 views
0

我试图添加一个“收藏夹”明星到现有的tableview列表。星星是一个按钮,存在于每个单元格内。当星星被按下时,我想将它包含的单元格添加到新的tableview中,从而创建一个收藏夹列表。尝试添加单元格从现有的tableview到一个新的tableview时按下按钮(添加到收藏夹按钮)

例如,如果按下按钮,将单元格添加到favoritesTableView。

有没有关于这个(我可以看到)的在线文档,但这是我迄今为止。

注:SearchResult所=收藏项目*

TableView.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 


    static NSString *strainTableIdentifier = @"StrainTableCell"; 

    StrainTableCell *cell = (StrainTableCell *)[tableView dequeueReusableCellWithIdentifier:strainTableIdentifier]; 
    if (cell == nil) 


     cell = [[[StrainTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strainTableIdentifier] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
     cell.selectionStyle = UITableViewCellSelectionStyleBlue; 



     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"StrainTableCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 

    if (tableView == self.searchDisplayController.searchResultsTableView) { 
     NSLog(@"Using the search results"); 

     cell.titleLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Title"]; 
     cell.descriptionLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Description"]; 
     cell.ratingLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Rating"]; 
     cell.ailmentLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Ailment"]; 
     cell.actionLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Action"]; 
     cell.ingestLabel.text = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Ingestion"]; 

     NSLog(@"%@", searchResults); 





    } else { 
     NSLog(@"Using the FULL LIST!!"); 
     cell.titleLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Title"]; 
     cell.descriptionLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Description"]; 
     cell.ratingLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Rating"]; 
     cell.ailmentLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Ailment"]; 
     cell.actionLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Action"]; 
     cell.ingestLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Ingestion"]; 

    } 


    NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row]; 
    cell.textLabel.text = [item objectForKey:@"text"]; 

    [item setObject:cell forKey:@"StrainTableCell"]; 
    BOOL checked = [[item objectForKey:@"checked"] boolValue]; 
    UIImage *image = (checked) ? [UIImage imageNamed:@"checked.png"] : [UIImage imageNamed:@"unchecked.png"]; 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    CGRect frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height); 
    button.frame = frame; 
    [button setBackgroundImage:image forState:UIControlStateNormal]; 
    [button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside]; 
    button.backgroundColor = [UIColor clearColor]; 
    cell.accessoryView = button; 

return cell; 


} 


- (void)checkButtonTapped:(id)sender event:(id)event 
{ 
    NSSet *touches = [event allTouches]; 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentTouchPosition = [touch locationInView:self.tableView]; 
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition]; 
    if (indexPath != nil) 
    { 
     [self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath]; 
    } 
} 



- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath 
{ 
    NSMutableDictionary *item = [dataArray objectAtIndex:indexPath.row]; 
    BOOL checked = [[item objectForKey:@"checked"] boolValue]; 
    [item setObject:[NSNumber numberWithBool:!checked] forKey:@"checked"]; 
    UITableViewCell *cell = [item objectForKey:@"StrainTableCell"]; 
    UIButton *button = (UIButton *)cell.accessoryView; 
    UIImage *newImage = (checked) ? [UIImage imageNamed:@"unchecked.png"] : [UIImage imageNamed:@"checked.png"]; 
    [button setBackgroundImage:newImage forState:UIControlStateNormal]; 
} 

回答

0

“我知道我会得到一些管理的评价是,这是不是一个答案”,但一个更简单的方法就是在用户决定要查看收藏夹后,重新载入您的表格视图和收藏夹信息数组。除此之外,你有什么问题,我会进一步重新编辑我的答案来帮助你,你根本没有得到任何结果?下面

将答案:

看起来像你的情况这符合您的需求

- (void)checkButtonTapped:(id)sender event:(id)event 
{ 
NSSet *touches = [event allTouches]; 
UITouch *touch = [touches anyObject]; 
CGPoint currentTouchPosition = [touch locationInView:self.tableView]; 
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint: currentTouchPosition]; 
if (indexPath != nil) 
{ 
[self tableView: self.tableView accessoryButtonTappedForRowWithIndexPath: indexPath]; 
} 

这是怎样的代码是http://www.edumobile.org/iphone/iphone-programming-tutorials/impliment-a-custom-accessory-view-for-your-uitableview-in-iphone/

和代码通过调用

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath 
结束

虽然你可以替换或添加你需要的代码那个方法。你需要的代码是创建一个NSMutablearray我假设你知道如果你不熟悉它,确保你在向它添加一些东西后运行一个nslog来测试它。一旦你有你的NSMutableArray的收藏夹,你会加入类似...

[myFavoritesArray addobject:[myOriginalArray objectAtIndex:indexPath]]; 

我不知道,如果你改变原来的表和收藏夹表,但如果他们实际上是相同的,那么你可以简单的重启之间的意见在按下按钮的表格中,除非您不想根据导航设置的方式切换视图的动画。希望这有助于。

+0

嗨!我只是想弄清楚如何创建收藏夹阵列。例如,一旦用户看到完整的咖啡馆列表,一旦用户点击“星级”按钮来收藏,我可以使用什么样的代码来告诉应用程序保存此收藏夹,然后将其加载到收藏夹表格中?谢谢! –

+0

根据您的需求更新了答案。 – rezand

+0

惊人的参考。好的,看看我上面更新的代码(.m文件)。出于某种原因,在执行此操作后,模拟器在我的else语句下为此行引发了SIGBRT错误:cell.titleLabel.text = [[Strains objectAtIndex:indexPath.row] objectForKey:@“Title”]; –

相关问题