birdcage
birdcage

Reputation: 2676

Swipe delete button goes under custom UITableViewCell text

I am having trouble with swipe delete button with custom UITableViewCell. When I swipe it, delete button goes under text in the cell. Below you can see the image and the code I am using.

Note: I draw tableviewcell in an xib.

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return YES if you want the specified item to be editable.
    return YES;
}

// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //add code here for when you hit delete
    }
}

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

    HareketCell *cell = (HareketCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"HareketCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];

    [cell.lblDakika setText:[NSString stringWithFormat:@"%@%@", [[arrProgramim objectAtIndex:indexPath.row]valueForKey:@"dakika"], @" dk"]];

    [cell.imgThumbnail setImage:[UIImage imageNamed:@"most_harita_icon.png"]];

    return cell;
}

enter image description here

Upvotes: 2

Views: 753

Answers (2)

galrito
galrito

Reputation: 352

That is because you are not adding the UILabel as a subview of the contentView of UITableViewCell, you are adding it as a subview of UITableViewCell itself.

When you want to subclass a UITableViewCell and adding subviews to it, you should always add them inside the contentView of the cell.

This is from Apple's documentation of UITableViewCell:

If you want to go beyond the predefined styles, you can add subviews to the contentView property of the cell.

If you do it like that, iOS automatically adjusts everything inside contentView to show that delete button and nothing overlaps.

Update:

From the comments below, the cell is being created on a separate .xib, so the label is already a subview of cell's contentView

The problem is the loading of the NIB in -tableView:cellForRowAtIndexPath:. You have to register the NIB on table view beforehand, in -viewDidLoad, for example. Like this:

- (void)viewDidLoad {
    // Assuming the xib is named HareketCell.xib
    UINib *nib = [UINib nibWithNibName:@"HareketCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"HareketCell"];
}

Now, change -tableView:cellForRowAtIndexPath: for this:

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

    HareketCell *cell = (HareketCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    [cell.lblDakika setText:[NSString stringWithFormat:@"%@%@", [[arrProgramim objectAtIndex:indexPath.row]valueForKey:@"dakika"], @" dk"]];

    [cell.imgThumbnail setImage:[UIImage imageNamed:@"most_harita_icon.png"]];

    return cell;
}

You see, -dequeueReusableCellWithIdentifier already loads the NIB for you automatically, if you register it beforehand.

Upvotes: 7

iOSDeveloper
iOSDeveloper

Reputation: 461

i think this might be because u r label is too long... keep your label short and then try .. i think after making your label short you dont see the label overlappin on the delete button. If u cant find an answer to this you can always use a 3rd party library called SWTableViewCell which can be found here https://github.com/CEWendel/SWTableViewCell

Upvotes: 0

Related Questions