如何在Objective-C中使用自定义单元格

我想在这个图像中在uiTableView中的一行中创build4个单元格“button或图片”:

但我不知道我该怎么做:

你能帮我吗? 提前致谢!

这里是我的代码:

- (void)viewDidLoad { [super viewDidLoad]; NSMutableArray *keys = [[NSMutableArray alloc] init]; NSMutableDictionary *contents = [[NSMutableDictionary alloc] init]; NSString *monKey = @"Monday"; NSString *tueKey = @"Tuesday"; NSString *wedKey = @"Wednday"; NSString *thuKey = @"Thusday"; NSString *friKey = @"Friday"; NSString *satKey = @"Satuarday"; NSString *sunKey = @"Sunnday"; [contents setObject:[NSArray arrayWithObjects:@"Work Time", @"Absence", nil] forKey:monKey]; [contents setObject:[NSArray arrayWithObjects:@"Compensation", @"Work Time", @"Absence", nil] forKey:wedKey]; [contents setObject:[NSArray arrayWithObjects:@"Compensation", @"Work Time", nil] forKey:tueKey]; [contents setObject:[NSArray arrayWithObjects:@"Compensation", @"Work Time", nil] forKey:thuKey]; [contents setObject:[NSArray arrayWithObjects:@"Compensation", @"Work Time", nil] forKey:friKey]; [contents setObject:[NSArray arrayWithObjects:@"Compensation", @"Work Time", nil] forKey:satKey]; [contents setObject:[NSArray arrayWithObjects:@"Compensation", @"Work Time", nil] forKey:sunKey]; [keys addObject:tueKey]; [keys addObject:monKey]; [keys addObject:wedKey]; [keys addObject:thuKey]; [keys addObject:friKey]; [keys addObject:satKey]; [keys addObject:sunKey]; [self setSectionKeys:keys]; [self setSectionContents:contents]; self.navigationItem.leftBarButtonItem = self.editButtonItem; UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNewItem)]; self.navigationItem.rightBarButtonItem = rightButton; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSString *key = [[self sectionKeys] objectAtIndex:[indexPath section]]; NSArray *contents = [[self sectionContents] objectForKey:key]; NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]]; static NSString *CellIdentifier = @"CellIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; } [[cell textLabel] setText:contentForThisRow]; int column = 4; for (int i=0; i<column; i++) { UIImageView *aBackgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(32+184*i,10, 167,215)]; aBackgroundImageView.tag = (column*indexPath.row)+i; [cell.contentView addSubview:aBackgroundImageView]; // [aBackgroundImageView release]; } return cell; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { if(section == 0) return @"Monday"; else if(section == 1){ return @"Tuesday"; }else if(section == 2){ return @"Wednesday"; } else if(section == 3){ return @"Thuesday"; } else if(section == 4){ return @"Friday"; } else if(section == 5){ return @"Saturday"; }else return @"Sunday"; } 

编辑1

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithFrame:CGRectZero]; } cell.selectionStyle = UITableViewCellSelectionStyleNone; int column = 4; for (int i=0; i<column; i++) { UIImageView *aBackgroundImageView = [[UIImageView alloc]initWithFrame:CGRectMake(32+184*i,10, 167,215)]; aBackgroundImageView.tag = (column*indexPath.row)+i; [cell.contentView addSubview:aBackgroundImageView]; } return cell; } 

你可以用Xcode中的4个buttondevise一个自定义的单元格,并将它们映射到tableview

如果你不需要用户交互,你可以将它们添加到一个UIView(每个是一个适当的框架的子视图),并使容器查看单元格的backgroundview。

您可以直接将视图添加到单元格的contentView并获得用户交互,但是如果有附属视图等,那么您必须小心地修改该区域。

devise自定义单元格会更容易,并在界面构build器中布置4个button(因为您需要用户交互)。 这是一个非常好的教程 ,让你开始。 给每个button分配一个标签号码,以便稍后知道哪个button被点击。

如下图所示在uiTableView中要在一行中创build4个单元格“button或图片”:

但我不知道我该怎么做:

首先,你需要创build一个名为MyCell的Objective-C类。 MyCell将是UITableViewCell的一个子类(这是非常重要的)在你的MyCell.h中,

  @interface MyCell : UITableViewCell 
 @property (nonatomic, weak) IBOutlet UIButton *firstButton; @property (nonatomic, weak) IBOutlet UIButton *secondButton; @property (nonatomic, weak) IBOutlet UIButton *thirdButton; @property (nonatomic, weak) IBOutlet UIButton *fourthButton; @end 

然后在你的MyCell.m中,综合所有这些button。

在你想要使用自定义单元格的tableview中,你需要修改cellForRowAtIndexPath方法。 将您的UITableViewCellreplace为您的自定义MyCell 。 别忘了将“MyCell.h”导入到您正在使用的tableview中。

  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(!cell) { cell = [[MyCell alloc]initWithStyle:UITableViewCellStyleDefault 

reuseIdentifier:CellIdentifier]; }

 [cell.firstButton setTintColor:[UIColor redColor]; [cell.secondButton setTintColor:[UIColor redColor]; // and so on.. til you set all your four buttons. return cell; } 

从那里,你的tableview应该显示你的自定义单元格。