从DetailView将单元格添加到“收藏夹”TableView

时间:2013-08-03 23:35:31

标签: ios objective-c uitableview uiviewcontroller uibutton

我有一个现有的UITableView,列出了该地区的一些咖啡馆。每个咖啡馆的数据都是从MySQL数据库中提取的。当用户点击咖啡馆(单元格)时,它会将用户带到详细视图。目前,用户可以通过单击每个单元格中的星形图像来“收藏”咖啡馆(这会将收藏的单元格添加到FavoritesTableView)。但是,我希望用户能够从DetailView中将一个咖啡馆添加到FavoritesTableView(换句话说,“最喜欢”来自DetailView的咖啡馆)。有谁知道我会如何实现这个?

现在,我的 DetailView.m (咖啡馆详情)中有星号按钮:

- (IBAction)buttonpressed:(UIButton *)fave {
    if (!checked) {
        [checkedButton setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
        checked = YES;

    }

    else if (checked) {
        [checkedButton setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
        checked = NO;
    }

}

ViewController.m (cafes tableview)

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

    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 = [Strains objectAtIndex:indexPath.row];
    cell.textLabel.text = [item objectForKey:@"text"];

    [item setObject:cell forKey:@"StrainTableCell"];
    BOOL checked = [[item objectForKey:@"checked"] boolValue];

    NSLog(@"%i",checked);
    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
{
    NSLog(@"made it here and event is %@",event);

    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    CGPoint currentTouchPosition = [touch locationInView:self.StrainTableView];
    NSIndexPath *  indexPath ;
    indexPath =  [self.StrainTableView indexPathForRowAtPoint: currentTouchPosition];
    NSLog(@"indexpath is below");
    NSLog(@"%@",indexPath);
    if (indexPath != Nil)
    {

        NSMutableDictionary *item = [Strains objectAtIndex:indexPath.row];
        BOOL isItChecked =  [[item objectForKey:@"checked"] boolValue];

NSMutableArray *quickArray = [[NSMutableArray alloc] initWithArray:Strains];
        [quickArray replaceObjectAtIndex:indexPath.row withObject:item];


        [item setObject:[NSNumber numberWithBool:!isItChecked] forKey:@"checked"];
        Strains = [quickArray copy];

 [StrainTableView reloadData];

    }

}

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {



        StrainDetailViewController *detailViewController = [[StrainDetailViewController alloc] initWithNibName:@"StrainDetailViewController" bundle:nil]; if ([searchResults count]) {

            detailViewController.title = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Title"];
            detailViewController.strainDetail = [searchResults objectAtIndex:indexPath.row];

        } else {

            detailViewController.title = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Title"];
            detailViewController.strainDetail = [Strains objectAtIndex:indexPath.row];
            NSLog(@"%@", Strains);
        }


        [self.navigationController pushViewController:detailViewController animated:YES];



        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }


    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];

        if ([[NSUserDefaults standardUserDefaults] objectForKey:@"strains"] != Nil) {


            NSData *dataSave = [[NSUserDefaults standardUserDefaults] objectForKey:@"strains"];
            Strains =  [NSKeyedUnarchiver unarchiveObjectWithData:dataSave];
        }


        if (favoritesArray == Nil) {
             favoritesArray = [[NSMutableSet alloc] init];   
        }



        if ([[NSUserDefaults standardUserDefaults] objectForKey:@"favorites"] != Nil) {


            NSData *dataSave = [[NSUserDefaults standardUserDefaults] objectForKey:@"favorites"];
            favoritesArray =  [NSKeyedUnarchiver unarchiveObjectWithData:dataSave];
        }

我只是不确定我要添加哪种代码才能使“Checked”按钮将所选单元格从UITableView添加到FavoritesTableView。

希望这是有道理的。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

您可以将“收藏”状态保存在全局变量/单例中,并在需要时检索它。

这样,即使您的表视图已取消分配,您也不会丢失其单元格的状态。

答案 1 :(得分:0)

从可维护性的角度来看,我通常采取的方法可能不是最好的。但是,从实施的角度来看,这很容易。我将对象的引用(在您的情况下,所选择的表视图的对象)传递给详细信息视图。从详细信息视图中,我可以通过切换标志来更新该对象。由于此对象与表视图是同一个对象,因此更新它也会更新表视图(您可能需要重绘表视图单元)。最后,我从详细信息视图更新数据库。

修改

所以在你的代码中,它看起来像这样。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    StrainDetailViewController *detailViewController = [[StrainDetailViewController alloc] initWithNibName:@"StrainDetailViewController" bundle:nil]; if ([searchResults count]) {

        detailViewController.title = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"Title"];
        //YOU'RE ALREADY DOING IT :)
        detailViewController.strainDetail = [searchResults objectAtIndex:indexPath.row];

    } else {

        detailViewController.title = [[Strains objectAtIndex:indexPath.row] objectForKey:@"Title"];
        //YOU'RE ALREADY DOING IT :)
        detailViewController.strainDetail = [Strains objectAtIndex:indexPath.row];
        NSLog(@"%@", Strains);
    }

    [self.navigationController pushViewController:detailViewController animated:YES];
}

您已经发送了对详情视图的引用!这让事情变得简单。在您的详细信息视图中,每当有人收到压力时,请为strainDetail对象设置收藏夹标记。这也将更新表视图中的strainDetail对象。它的工作方式与此类似,因为您将引用(也称为指针)传递给详细视图。换句话说,你的strainDetail对象都不是对象,而是指向内存地址的指针。如果更新strainDetail,则两个strainDetail的内存地址都会更改。不是strainDetail指针本身。以下是进一步说明https://softwareengineering.stackexchange.com/questions/17898/whats-a-nice-explanation-for-pointers

的链接

因此,您的详细信息视图处理程序将类似于此

- (IBAction)buttonpressed:(UIButton *)fave {
    if (!checked) {
        [checkedButton setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
        checked = YES;

    }

    else if (checked) {
        [checkedButton setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
        checked = NO;
    }

    //updates in both the detail view and the table view
    BOOL isItChecked =  [[self.strainDetail objectForKey:@"checked"] boolValue];
    [self.strainDetail setObject:[NSNumber numberWithBool:checked] forKey:@"checked"];
}

我还建议创建一个Strain类来保存所有的应变数据。使用词典并不好玩,容易出错,并且需要永远找出你需要的键。