如何在objective-c中使用自定义单元格 [英] how to use custom cell in objective-c

查看:20
本文介绍了如何在objective-c中使用自定义单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

但我不知道我该怎么做:

请帮帮我好吗!提前致谢!

这是我的代码:

- (void)viewDidLoad{[超级viewDidLoad];NSMutableArray *keys = [[NSMutableArray alloc] init];NSMutableDictionary *contents = [[NSMutableDictionary alloc] init];NSString *monKey = @"星期一";NSString *tueKey = @"星期二";NSString *wedKey = @"星期三";NSString *thuKey = @"星期四";NSString *friKey = @"星期五";NSString *satKey = @"星期六";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目标:自我行动:@选择器(添加新项目)];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]];静态 NSString *CellIdentifier = @"CellIdentifier";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];如果(单元格 == 零){cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier];}[[cell textLabel] setText:contentForThisRow];整数列 = 4;for (int i=0; i

编辑 1

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{静态 NSString *CellIdentifier = @"Cell";UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];如果(单元格 == 零){cell = [[UITableViewCell alloc] initWithFrame:CGRectZero];}cell.selectionStyle = UITableViewCellSelectionStyleNone;整数列 = 4;for (int i=0; i

解决方案

想在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.

这篇关于如何在objective-c中使用自定义单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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