2

I have UITableView in my application.

Attributes: TableStyle = Plain, SeperatorStyle = single and SeperatorColor = black

What I want to do is that, I want to put a image (which a small strip), as separator for the table.

Please help me.

Regards, Pratik

pratik
  • 4,419
  • 8
  • 41
  • 65

3 Answers3

23

Solution which worked for me.

self.tableView.separatorColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"yourImage"]];
ppolak
  • 641
  • 5
  • 8
  • 3
    Note that colorWithPatternImage is really awful from a memory usage perspective, and you should carefully consider before using it. See http://www.cocoaintheshell.com/2011/01/colorwithpatternimage-memory-usage/ for more information. A thin UIImageView with the separator image in a custom table cell works just fine. – JK Laiho Sep 19 '12 at 09:12
6

So, usually with an iPhone table, you should just use one of the built in styles: http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instp/UITableView/separatorStyle

As an iPhone developer who has been down this road, I suggest you avoid images wherever possible, and use the API to build your app.

If you insist on your reckless course, or if you have a client demanding this or something, then what you can do is subclass UITableViewCell.

In your UITableViewCell subclass, you can add whatever UI elements you want to the contentView of the cell, including a separator image at the bottom of the cell.

So, here is an implementation of the delegate function that returns a table cell using a custom UITableViewCell:

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

  static NSString *CellIdentifier = @"Cell";

  // notice I use a SavedTableCell here instead of a UITavbleViewCell
  SavedTableCell *cell = (SavedTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

  if (cell == nil) {
    cell = [[[SavedTableCell alloc] initWithReuseIdentifier:CellIdentifier hasIcon:YES] autorelease];
  }

  // custom function to set the fields in the cell
  [cell setItem:[self.items objectAtIndex:indexPath.row]];  
  return cell;
}

And then here's my simplified implementation of SavedTableCell.h:

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

@interface SavedTableCell : UITableViewCell {

  // my cell has one label and one image, but yours would have an image placed at the bottom of the cell's frame
  UILabel *primaryLabel;
  UIImageView *icon;
}

// i just made up the class Item... in my code they are actually Waypoints.
- (void) setItem:(Item*)item;


@property(nonatomic,retain) UILabel *primaryLabel;
@property(nonatomic,retain) UIImageView *icon;


@end

And here is a simplified SavedTableCell.m:

#import "SavedTableCell.h"

@implementation SavedTableCell

@synthesize primaryLabel, icon;


- (id)initWithReuseIdentifier:(NSString *)reuseIdentifier {
  if (self = [super initWithStyle:UITableViewStylePlain reuseIdentifier:reuseIdentifier]) {
    icon = [[UIImageView alloc] initWithFrame:CGRectMake(5,5,40,40)];
    [self.contentView addSubview:icon];
    primaryLabel = [[UILabel alloc]init];
primaryLabel.font = TITLE_FONT;     
    [self.contentView addSubview:primaryLabel]; 
  } 
  return self;
}


- (void) setItem:(Item*)item {
  self.primaryLabel.text = [item name];
  [self.icon setImage:[item imageName]];
}

- (void)layoutSubviews {
  [super layoutSubviews];
  primaryLabel.frame = LABEL_FRAME;
  icon.frame = ICON_FRAME;  
}

- (void)dealloc {
  [icon release];
  [primaryLabel release];
}


@end
Andrew Johnson
  • 13,108
  • 13
  • 75
  • 116
  • @Andrew: By subclass UITableViewCell, you mean to say "cellForRowAtIndexPath" method ??. Also, how to determine bottom of the cell ? – pratik Feb 09 '10 at 07:29
  • @Andrew: Also can you tell in detail about: "As an iPhone developer who has been down this road, I suggest you avoid images wherever possible, and use the API to build your app." – pratik Feb 09 '10 at 07:34
  • I added code that shows you how to do a custom UITableViewCell. If you want to place an image at the bottom of the cell as a separator, then just set its frame appropriately. So, if your table cell was 40 px high and 320px wide, then your image's frame would be CGRectMake(0,40-image-size.height,image.size.width,image.size.height); – Andrew Johnson Feb 09 '10 at 07:40
  • What I mean by the other thing is there is no reason to make a custom table separator. Just use Apple's built in line. And whenever you find yourself doing something with images like this, or having a hard time because you want to customize something built in, just don't. First of all, your users want your app to be iPhonic... they expect it to look standard and if it doesn't your sales will suffer. Second, isn't there a better way to spend your time than trying to customize the line separator of a table? I did a lot of customization like this in my first app and it was a big mistake. – Andrew Johnson Feb 09 '10 at 07:43
  • I should also caution you not to use transparent images or alpha transparency on any of the subviews of your UITableViewCell. This will horribly slow down your app, particularly on 3G phones. – Andrew Johnson Feb 09 '10 at 07:44
  • 1
    "[...]there is no reason to make a custom table separator." Incorrect. Your comment was from 2010 and things have evolved since then (though I'd argue it was incorrect back then, too). Given Apple's efforts in iOS 5 and 6 to allow for easier UI customizations, and the emergence and popularity of gorgeous, heavily customized apps like TweetBot, there absolutely is a reason to go custom with elements like table cell separators when a design calls for it. – JK Laiho Sep 19 '12 at 09:15
  • 1
    I agree with @JKLaiho - the use of images and transparencies in UITableViewCells is no longer an issue. In 2010, people had iPhone 3G phones - none of the modern iPhones will have stuttering tableViews unless you really have complex, large images. I also used to do things like pre-instantiate views, and use behavioral delegates so I could change the behavior of classes withour re-making them - but all those hoops are for the bygone days of iOS development. – Andrew Johnson Sep 19 '12 at 23:48
0

I want just to add some thoughts to Andrew Johnson's answer.

If you add subview to contentView and use accesoryView or image,then it'll show separator image incorrectly. Use something like

UIView *bg = [[UIView alloc] initWithFrame:CGRectMake(5, 5, 40, 40)];
    bg.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.2];
                    //or [UIColor colorWithImagePattern];
                    //or crete UIImageView and assign to backgroundView of the cell
self.backgroundView = bg;
[bg release];
Alexander
  • 1,228
  • 2
  • 15
  • 29