在“收藏夹表视图”中取消选中(不相关)项目

时间:2013-08-22 05:38:58

标签: objective-c uitableview uibutton

我有一个tableview,显示“favited”项目列表。另一个tableview中的用户喜欢的项目,以及此tableview(FavoritesTableView.m)中列出的收藏项目。

出于某种原因,我无法从FavoritesTableView中“取消选中”检查项目(收藏项目)?我错过了什么? 请参阅下面的.m文件...

FavoritesViewController.h

        #import <UIKit/UIKit.h>
        #import "StrainTableCell.h"

        @interface FavoritesViewController : UITableViewController
        {

        }
        @property (strong, nonatomic) NSArray *favoritesArrayset;
        @property (strong, nonatomic) IBOutlet UITableView *favoritesTable;
        @property (nonatomic, strong) NSMutableArray * favoritesArray;

    - (IBAction)backbuttonpressed: (UIBarButtonItem *)sender;


    @end

FavoritesViewController.m

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

        if (favoritesArray == Nil)
        {
            favoritesArray = [[NSMutableArray alloc] init];
        }
        else
        {
            [favoritesArray removeAllObjects];
        }

        NSData *dataSave = [[NSUserDefaults standardUserDefaults] objectForKey:@"strains"];

        if (dataSave != Nil)
        {
            favoritesArrayset = [NSKeyedUnarchiver unarchiveObjectWithData:dataSave];

            for (NSDictionary *item in favoritesArrayset)
            {
                BOOL isChecked = [[item objectForKey:@"checked"] boolValue];
                if (isChecked == YES )
                {
                    [favoritesArray addObject:item];
                }
            }
        }

        [favoritesTable reloadData];
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];
    }

    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
    }

    #pragma mark - Table view data source
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 1;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return favoritesArray.count;
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        NSArray *Strains = [favoritesArray copy];
        NSArray *dataArray = [favoritesArray copy];

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

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

            cell.whatCellamI = [NSNumber numberWithInt:indexPath.row];

            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)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        StrainDetailViewController *detailViewController = [[StrainDetailViewController alloc]
                                                            initWithNibName:@"StrainDetailViewController" bundle:nil];
        detailViewController.title = [[favoritesArray objectAtIndex:indexPath.row] objectForKey:@"Title"];
        detailViewController.strainDetail = [favoritesArray objectAtIndex:indexPath.row];
        [self.navigationController pushViewController:detailViewController animated:YES];
    }

    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        return 82;
    }

    - (IBAction)backbuttonpressed:(id)sender
    {
        [self.view.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
    }

 - (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.favoritesTable];
    NSIndexPath *  indexPath ;
    indexPath =  [self.favoritesTable indexPathForRowAtPoint: currentTouchPosition];
    NSLog(@"indexpath is below");
    NSLog(@"%@",indexPath);
    if (indexPath != Nil)
    {

        NSMutableDictionary *item = [favoritesArray objectAtIndex:indexPath.row];
        BOOL isItChecked =  [[item objectForKey:@"checked"] boolValue];
        /*
         if (isItChecked == NO) {
         NSMutableArray *tmpArray = [[NSMutableArray alloc] init];
         NSMutableArray *tmpArray2 = [[NSMutableArray alloc] initWithArray:[favoritesArray allObjects]];
         NSString *text1 = [item objectForKey:@"Title"];
         for (NSDictionary * object in tmpArray2) {
         NSString *text2 = [object objectForKey:@"Title"];
         if (![text1 isEqualToString:text2]) {
         [tmpArray addObject:object];
         }
         }
         // [favoritesArray removeAllObjects];
         favoritesArray = [tmpArray copy];

         }
         */


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


        [item setObject:[NSNumber numberWithBool:!isItChecked] forKey:@"checked"];
        favoritesArray = [quickArray copy];
        //  [self.favoritesArray addObject:item];
        // NSLog(@"you have added %d items to favorites", self.favoritesArray.count);
        [favoritesTable reloadData];


    }


    @end

2 个答案:

答案 0 :(得分:1)

在你的.h中取一个NSMutableDictionary并为它做属性。

在你的.m中合成它,并在viewdidLoad中分配字典。

现在将此代码放在CellForRowAtIndex

  if([idDictonary objectForKey:[NSString stringWithFormat:@"%d",[indexPath row]]])
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    }

    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

并将此代码放在DidSelectRowAtIndex

UITableViewCell *thisCell = [tableView cellForRowAtIndexPath:indexPath];

if (thisCell.accessoryType == UITableViewCellAccessoryNone)
{
    thisCell.accessoryType = UITableViewCellAccessoryCheckmark;
    [idDictonary setValue:[dataArray objectAtIndex:indexPath.row] forKey:[NSString stringWithFormat:@"%d",[indexPath row]]];
}
else
{
    if([idDictonary objectForKey:[NSString stringWithFormat:@"%d",[indexPath row]]])
    {
        [idDictonary removeObjectForKey:[NSString stringWithFormat:@"%d",[indexPath row]]];

        thisCell.accessoryType = UITableViewCellAccessoryNone;
    }

}
[myTableView reloadData];
NSLog(@"idDictonary = %@",idDictonary);

我希望这会有助于布列塔尼......

答案 1 :(得分:0)

每次-tableView:cellForRowAtIndexPath:运行时,您都会创建一个新按钮,将其设置为所需的属性并将其丢弃。 (本周似乎是个笑话。)你必须使用现有的按钮。