iPhone + UITableView +为分隔符放置图像 [英] iPhone + UITableView + place an image for separator

查看:147
本文介绍了iPhone + UITableView +为分隔符放置图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有UITableView。

I have UITableView in my application.

属性:TableStyle = Plain,SeperatorStyle = single和SeperatorColor = black

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.

请帮助我。

问候,
Pratik

Regards, Pratik

推荐答案

所以,通常与在iPhone桌面上,您应该只使用其中一种内置样式: http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/ instp / UITableView / separatorStyle

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

作为一名走在这条路上的iPhone开发人员,我建议您尽可能避免使用图片,并使用API​​构建您的app。

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.

如果哟你坚持你鲁莽的课程,或者你有客户要求这个或者什么,那么你可以做的是UITableViewCell的子类。

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.

在你的UITableViewCell子类中,你可以添加你想要的任何UI元素单元格的contentView,包括单元格底部的分隔符图像。

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.

所以,这里是一个委托函数的实现,返回使用自定义UITableViewCell的表格单元格:

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;
}

然后这是我对SavedTableCell.h的简化实现:

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

这是一个简化的SavedTableCell。 m:

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

这篇关于iPhone + UITableView +为分隔符放置图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆