且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在objective-c中使用自定义单元格

更新时间:2022-12-10 11:47:26

想在uiTableView的一行中创建4个单元格按钮或图片"如下图:

但我不知道我该怎么做:

首先,您需要创建一个名为 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;@结尾

然后在您的 MyCell.m 中,合成所有这些按钮.

在您想要使用自定义单元格的 tableview 中,您需要修改 cellForRowAtIndexPath 方法.用您的自定义 MyCell 替换您的 UITableViewCell.并且不要忘记将MyCell.h"导入您正在使用的那个tableview.

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {静态 NSString *CellIdentifier = @"Cell";MyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];如果(!单元格){cell = [[MyCell alloc]initWithStyle:UITableViewCellStyleDefault

reuseIdentifier:CellIdentifier];}

[cell.firstButton setTintColor:[UIColor redColor];[cell.secondButton setTintColor:[UIColor redColor];//依此类推.. 直到您设置所有四个按钮.返回单元格;}

从那里开始,您的 tableview 应该显示您的自定义单元格.

I want to create 4 cell "button or picture" in one row in uiTableView like this picture:

but I don't know how can I do that :

would you please help me! Thanks in advance!

here is my code :

- (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";

}

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

want to create 4 cell "button or picture" in one row in uiTableView like this picture:

but I don't know how can I do that :

First of all, you will need to create an objective-c class called MyCell. MyCell will be a subclass of UITableViewCell (this is very important) In your 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

Then in your MyCell.m, just synthesize all those buttons.

In your tableview, where you want to use your customized cell, you need to modify the cellForRowAtIndexPath method. Replace your UITableViewCell with your custom MyCell. And don't forget to import "MyCell.h" into that tableview that you are using.

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

And from there, your tableview should be showing your custom cell.