NSUser
NSUser

Reputation: 1233

Adding custom edit and delete button in UITableViewCell

I have a UITableView which contains the names of all countries. The user can delete or edit the name of country anytime by taping on the cell.

My UITableView cell initially looks like this:

enter image description here

Now when user taps on it I am changing it like this:

enter image description here

I think I am following a very lame approach.Here is what I did:

Declared globally buttons to add:

 UIButton *btnDeleteWithImage,*btnDeleteWithText,*btnEditWithImage,*btnEditWihtText; //buttons

And a NSMutableArray to keep track of indexPath.row

Now in my didSelectMethod I am doing this:

 //To change the background 
 UIView *selectionBackground = [[UIView alloc] init];

  selectionBackground.backgroundColor = [UIColor customColor];

 cell.selectedBackgroundView = selectionBackground;

 // to check which cell is pressed

if([indexPathCollection containsObject:index])
{
    [btnDeleteWithImage removeFromSuperview];
    [btnDeleteWithText removeFromSuperview];
    [btnEditWihtText removeFromSuperview];
    [btnEditWithImage removeFromSuperview];
    [indexPathCollection removeObject:index];

    [cell addSubview:btnDeleteWithImage];
    [cell addSubview:btnDeleteWithText];
    [cell addSubview:btnEditWithImage];
    [cell addSubview:btnEditWihtText];
    [indexPathCollection addObject:index];
}
else
{

    [cell addSubview:btnDeleteWithImage];
    [cell addSubview:btnDeleteWithText];
    [cell addSubview:btnEditWithImage];
    [cell addSubview:btnEditWihtText];
    [indexPathCollection addObject:index];
}

But this is not working good.When I scroll table edit and delete button randomly occurs.

Did someone has better Idea how can achieve this in a very efficient way.

Upvotes: 1

Views: 1034

Answers (2)

sargeras
sargeras

Reputation: 179

You can achieve this by creating a custom cell with your properties

CustomCell.h

@interface CustomCell : UITableViewCell

@property (nonatomic, strong) UIButton *btnDeleteWithImage;
@property (nonatomic, strong) UIButton *btnDeleteWithText;
@property (nonatomic, strong) UIButton *btnEditWithImage;
@property (nonatomic, strong) UIButton *btnEditWithText;

@end

initialize them in cell's init method keeping them hidden at first or you can do

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

    static NSString *customCellIdentifier = @"customCellIdentifier";
    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:customCellIdentifier];

    if (!cell) {
        cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:customCellIdentifier];
        // or you can initialize and add them to the cell here
    }
    //here you can modify them accordingly
    return cell;
}

then the delegate method can be

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

    CustomCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell.btnDeleteWithImage setHidden:NO];
    [cell.btnEditWithImage setHidden:NO];
}

Upvotes: 2

Garry
Garry

Reputation: 407

the simplest way is that do not reuse the cell . & register your custom cell with same indentifire .

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

customCell* cell = [[customCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:@"indentifire"] ;                                      

// manage your plooting here ..

return cell;
}

hope it works for you.

Upvotes: 1

Related Questions