UITableViewCell Custom Add to favourites button issue

997 Views Asked by At

in my table view, i placed a custom "Add to Favs" button in every cell to enable the user to copy the exact cell content to a second tableview controller. when you hit the "Add to Favs" button an alert view shows up to ask if you want to copy the cell and paste it to the second view controller or not. now there are two things. 1- is there a way to delete the "Add to Favs" button permanently from that cell if "OK" is selected from the alert view to indicate that the cell is copied and pasted to the second tableview? - so the user won't be able to add the cell content over and over again. 2- this is the bigger question: how would i copy and paste the cell content to the secondtableview controller with "Add to Favs" click?

here is the way my cells re configured:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    NSString* letter = [letters objectAtIndex:indexPath.section];
    NSArray* arrayForLetter = (NSArray*)[filteredTableData objectForKey:letter];
    Songs* songs = (Songs*)[arrayForLetter objectAtIndex:indexPath.row];

    cell.textLabel.text = songs.name;
    cell.detailTextLabel.text = songs.description;

    CGSize itemSize = CGSizeMake(50, 50);
    UIGraphicsBeginImageContextWithOptions(itemSize, NO, UIScreen.mainScreen.scale);
    CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
    [cell.imageView.image drawInRect:imageRect];
    cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    UIButton *addtoFavsButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    addtoFavsButton.frame = CGRectMake(200.0f, 5.0f, 105.0f, 70.0f);
    [addtoFavsButton setImage:[UIImage imageNamed:@"fav.png"] forState:UIControlStateNormal];
    [addtoFavsButton setTintColor:[UIColor whiteColor]];
    [cell addSubview:addtoFavsButton];
    [addtoFavsButton addTarget:self
                        action:@selector(addtoFavs:)
              forControlEvents:UIControlEventTouchUpInside];
    return cell;
    }
- (IBAction)addtoFavs:(id)sender
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Dikkaaaat!"
                                                   message:@"Şarkıyı Favori Akorlarınıza Alıyorsunuz..."
                                                  delegate:nil
                                         cancelButtonTitle:@"Cancel"
                                         otherButtonTitles:@"OK", nil];
            [alert show];
    }

enter image description here enter image description here

4

There are 4 best solutions below

0
On

Considering the data is in the app only, and considering you are using a regular table cell, not a custom one, what I would recommend is:

  • Make a new simple array in your app called "myFavorites" or whatever, which will hold just a list of numerical values.

  • When the user confirms to add to favorites, take the current section and row indexes from the songs array, and store in this new array.

  • In your first view controller, add to your "cellForRowAtIndexPath" a simple check to see if the song for that row exist in the new array with.

Something like:

 if([[myFavorites objectAtIndex:indexPath.section] containsObject:[NSString stringWithFormat:@"%ld", (long)indexPath.row]]){
     // Don't make button because is already a favorite.
 }else{
     // Make button because not yet a favorite.
 }

Your second table view will be almost identical, except near the top of your "cellForRowAtIndexPath", do a similar check:

if([[myFavorites objectAtIndex:indexPath.section] containsObject:[NSString stringWithFormat:@"%ld", (long)indexPath.row]]){
     // Is a favorite, so put regular cell stuff here to have the cell show
     ....

 }else{
     // Not a favorite, don't generate cell.
     cell.visible = NO;
     return cell;
 }

There are other things you can do but it would require changing your setup from a regular cell to a custom cell with a new class and properties blah blah, so is a little more complicated to implement, but would still amount to practically the same output/processing.

2
On

Firstly, you aren't copying and pasting - you're referencing. Specifically you're saying that some of your songs are special.

Secondly, the user should be able to tell if they're special, and be able to toggle it. Dispense with the alert, just show the state on the button and toggle the special setting on and off as its tapped.

Now, the second table view works in th same way as the first, it just filters the songs to decide what to display.

You need to decide how to mark each song as special, probably by adding a Boolean property to the class and saving it with the rest of the data. An alternative would be to have a separate list of song IDs (or unique names).

0
On

If you want to change you cell, you need make the custom view as a @property.

@property (nonatomic, strong) UIView *cellContent;

then setup in cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }
    [cell.contentView addSubview:[self setupCellContentView:indexPath]];
    return cell;
}

- (UIView *)setupCellContentView:(NSIndexPath *)indexPath
{
    if (!_cellContent) {
        _cellContent = [[UIView alloc] initWithFrame:CGRectZero];
    }
    // do sth like you did in cellForRowAtIndexPath//
    return _cellContent;
}

then you can manipulate the cell.contentView in alertView:clickedButtonAtIndex: and pass the view @property (nonatomic, strong) UIView *cellContent to next viewController

ps:After you remove the "Add to Favs" button from the _cellContent,don't forget

[_cellContent removeFromSuperview];

[tableView reloadData];
0
On

To answer the first question, YES you can remove the "Add to Favs" button from cell object but it will create issue with you rest of the songs display because of the fact that UITableView reuses the cell objects. So if you mark a cell favourite and remove it's button, any upcoming row which will reuse this object will not be able to show that favourite button to user. So you better discard this approach.

To maintain or copy the content of a cell, which is nothing but a reference to a Songs object in you master array, you can create another array of fav songs and add those Songs objects to this array. Now you can remove this song object from your master array and reload the table data. This approach is suitable if you are using 2 different table views for data display.

If you are displaying both type of songs in one table view by indicating through "Fav Icon", then you should add a BOOL property to Songs model object and set it when you confirm with alert view.