我想在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;
}
答案 0 :(得分:0)
如果您不需要用户交互,可以将它们添加到UIView(每个都是具有适当框架的子视图),并使容器查看单元格的backgroundview。
您可以将视图直接添加到单元格的contentView并获取用户交互,但是如果有一个accessoryView等,则必须小心修改该区域。
答案 1 :(得分:0)
您可以在Xcode中设计带有4个按钮的自定义单元格,并将它们映射到tableview
答案 2 :(得分:0)
在界面构建器中设计自定义单元格并布置4个按钮(因为您需要用户交互)会更容易。这是一个非常好的tutorial让你入门。为每个按钮指定一个标签号,这样您就可以知道以后点击了哪个按钮。
答案 3 :(得分: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中,只需合成所有这些按钮。
在您的tableview中,您要使用自定义单元格,您需要修改cellForRowAtIndexPath方法。 将您的UITableViewCell替换为您的自定义MyCell 。并且不要忘记将“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
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应该显示你的自定义单元格。