当按下按钮时,尝试将现有tableview中的单元格添加到新的tableview(添加到收藏夹按钮)

时间:2013-06-22 22:33:46

标签: objective-c view uitableview xcode4.5

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

E.g。如果按下按钮,则将单元格添加到favoritesTableView。

接下来没有在线文档(我可以看到),但这是我到目前为止所拥有的。

注意:searchResults =收藏的商品*

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];
}

1 个答案:

答案 0 :(得分: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]];

我不确定你是否正在改变原始表和收藏夹表之间的视图,但如果它们几乎相同,那么只需按一下按钮即可重新加载表,除非你不想切换视图的动画当然取决于如何设置导航。希望这有帮助。