Custom Delete For Custom UITableViewCell *solved*

Discussion in 'iPhone/iPad Programming' started by Darkroom, Aug 30, 2009.

  1. Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    the visual design of my custom table view doesn't accommodate apple's built-in delete editing style. therefore, i'm attempting to create my own delete function.

    within the custom cell's nib, i've created a custom delete UIButton that will become visible when the user enters my app's table view delete mod. the image of the delete button is 22x22, but the actual button has a greater size to make touching easier. the first thing i've noticed is that these buttons are very difficult to press. if i make the button larger, the cells seem to get confused (pressing the delete button on the first cell activates the delete on the 2nd cell). interestingly, this seems to only occur if my custom delete button is located at the very right edge of my custom cell. move it to the right appears to solve the issue. moving on.

    from my custom cell class, i've added an IBOutlet to the tableView's delegate and an IBAction for the delete button, and wired up the connection in IB:

    Code:
    #import <UIKit/UIKit.h>
    @class TableViewDelegate;
    
    @interface CustomCell : UITableViewCell
    	{
    	TableViewDelegate *tableViewDelegateClass;
    	UILabel *label1;
    	UILabel *label2;
    	UIButton *deleteButton;
    	}
    	
    @property (nonatomic, retain) IBOutlet UILabel * label1;
    @property (nonatomic, retain) IBOutlet UILabel * label2;
    @property (nonatomic, retain) IBOutlet UIButton *deleteButton;
    
    - (IBAction)deleteButton:(id)sender atIndexPath:(NSIndexPath *)indexPath;
    
    @end
    
    #import "CustomCell.h"
    #import "TableViewDelegate.h"
    
    @implementation SizeMeMeasurementsCell
    @synthesize label1, label2, deleteButton;
    
    
    - (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier
    	{
    	if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier])
    		{
    		// Initialization code
    		}
    	return self;
    	}
    	
    - (IBAction)deleteButton:(id)sender atIndexPath:(NSIndexPath *)indexPath
    	{
    	NSLog(@"Action Triggered");
    	[tableViewDelegateClass deleteTableViewCell:indexPath];
    	}
    
    - (void)dealloc
    	{
    	[label1 release];
    	[label2 release];
    	[deleteButton release];
    	[super dealloc];
    	}
    
    @end
    
    so now in my tableViewDelegate class, my deleteTableViewCell method currently looks like this:

    Code:
    - (void)deleteTableViewCell:(NSIndexPath *)indexPath
    	{
    	NSLog(@"Message Received From Custom Cell");
    	NSUInteger row = [indexPath row];
    	[self.listDataArray removeObjectAtIndex:row];
    	[self.myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    	}
    
    first problem: the deleteTableViewCell method is not receiving the message from the custom cell class. I'm assuming this is due to an error on my part, and i'm hoping it's not because my custom cell's XIB bypasses the use File's Owner as it's just a subclass of UITableViewCell. if this is the case, i hope there is a simple solution.

    because my connections aren't working, i can't test the affectiveness of my deleteTableViewCell:(NSIndexPath *)indexPath method. in this method i'm removing the objects at index path, both from the table view and the data source. i'm not sure if i'm also to update the tableView/data source in this method, or if it's automatic, but that should be easy enough to figure out.

    so. why is my table view delegate class not receiving the message from my UITableViewCell subclass, and does my deleteTableViewCell:(NSIndexPath *)indexPath appear to be correct?
     
  2. thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #2
    connections now work. i just added a proxy object representing my tableView delegate class, and connected the delete buttons to a method located there.

    Code:
    - (IBAction)deleteMeasurement:(id)sender atIndexPath:(NSIndexPath *)indexPath
    	{
    	NSLog(@"Message From Custom Cell Received");
    	NSUInteger row = [indexPath row];
    	[self.listDataArray removeObjectAtIndex:row];
    	[self.myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    	}
    
    this code is throwing an exception. problem with NSIndexPath. how can i pass the indexPath of the delete button from the custom UITableViewCell class to it's delegate?
     
  3. thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #3
    solved:

    1. Custom cell XIB is composed of a UIButton (and some UILabels), within a UIView, placed in the custom cell window.
    2. An External Object replaces the File's Owner proxy (just delete it), and set to represent the class that is the UITableView's delegate.
    3. The UIButton's touchUpInside (or whatever you want) is connected to the proxy object's IBAction (the following method) within UITableView's delegate class.

    Code:
    - (IBAction)deleteMeasurement:(id)sender
    	{
    	NSLog(@"Message From Custom Cell Received");
    	NSIndexPath *indexPath = [self.myTableView indexPathForCell:(UITableViewCell *)[[[sender superview] superview] superview]];
    	NSUInteger row = [indexPath row];
    	[self.myDataArray removeObjectAtIndex:row];
    	[self.myTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    	}
    
     
  4. macrumors newbie

    Joined:
    Jan 17, 2008
    #4
    I followed your instructions, and the delete method still isn't firing.
    Did you have to do anything special when you loaded the nib, like using the options: parameter?

    Code:
    		NSDictionary *proxy = [NSDictionary dictionaryWithObject:self forKey:@"cellProxyKey"];
    		NSDictionary *options = [NSDictionary dictionaryWithObject:proxy forKey:UINibExternalObjects];
    		NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:nil options:options];
    
     

Share This Page