0

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

但我不知道该怎么做:

你能帮帮我吗!提前致谢!

这是我的代码:

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

4 回答 4

0

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

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

于 2012-07-30T23:58:57.050 回答
0

您可以在 Xcode 中设计一个带有 4 个按钮的自定义单元格并将它们映射到 tableview

于 2012-07-31T06:45:45.417 回答
0

在界面构建器中设计自定义单元格并布置 4 个按钮(因为您需要用户交互)会更容易。这是一个很好的教程,可以帮助您入门。为每个按钮指定一个标签号,以便您知道稍后点击了哪个按钮。

于 2012-07-31T08:12:46.870 回答
0

想在 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;
  @end

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

在您想要使用自定义单元格的表格视图中,您需要修改 cellForRowAtIndexPath 方法。用您的自定义 MyCell 替换您的 UITableViewCell。并且不要忘记将“MyCell.h”导入您正在使用的表格视图中。

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

if(!cell)
{
    cell = [[MyCell alloc]initWithStyle:UITableViewCellStyleDefault

重用标识符:细胞标识符];}

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

return cell; }

从那里,您的表格视图应该显示您的自定义单元格。

于 2012-08-01T13:56:12.527 回答