我想像这个自定义的tableview一样实现。
有没有人可以教我如何实现这个tableview方法?
早上" ,"下午","晚上"标签必须放左。
然后标签后面有许多不同的按钮。
如果一行有超过3个按钮。它会放下一行(行)(如下面的照片"下午")。
我知道日期是使用标题标题。然后早上,下午,夜间使用了部分。
但我不知道如何实施"早晨","下午" ,"晚上"标签放左,然后按钮放右。 关键问题是我不能设置下午(早上或晚上)小组。
有没有人可以给我一些提示,或者一些教程如何动态设置(下午,早上或晚上)小组?
非常感谢!!!!! <(_ _)>
#pragma mark - Table view
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return [listResultDataAry count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
// 3 is morning , afternoon , night. But I think it will dynamic caculate to implement every label group.
return 3;
}
// header title text
-(NSString *)tableView:(UITableView*) tableView titleForHeaderInSection:(NSInteger)section
{
return [[listResultDataAry objectAtIndex:section] valueForKey:@"date"];
}
// table view cell content show
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ReuseCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
if( cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UILabel *dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 9,150, 25)];
switch (indexPath.row) {
case 0:
timeLabel.text = [[timeSegListAry objectAtIndex:0] valueForKey:@"time"];
break;
case 1:
timeLabel.text = [[timeSegListAry objectAtIndex:1] valueForKey:@"time"];
break;
case 2:
timeLabel.text = [[timeSegListAry objectAtIndex:2] valueForKey:@"time"];
break;
default:
break;
}
if(indexPath.section == 0 )
{
[cell.contentView addSubview:timeLabel];
}
else
{
[dateLabel removeFromSuperview];
}
return cell;
}
答案 0 :(得分:1)
对不起迟到的回答但如果按钮数超过3,则不需要使用不同的行,这将是tableview的复杂管理,相反,为什么不使用不同高度的唱行与所有按钮可以放在tableview单元格内的适当位置。
看下面的代码你可以在单独的项目中尝试它,因为自定义单元格不使用默认单元格“UITableViewCell”而是子类它,你可以轻松管理单元格内容(我建议)
在子类的.m文件中(我拿了一个日期字符串,数组包含按钮标题),这是你的时间
#import <UIKit/UIKit.h>
//you need to use the custom cell
@interface CustomCell : UITableViewCell //subclass the UITableViewCell for complete customization
@property (nonatomic, retain) NSString *titleString;//string for mrng or eve or nyt
@property (nonatomic, retain) NSArray *buttons;//for your buttons
@end
<。>文件中的
#import "CustomCell.h"
@implementation CustomCell
//synthesize them
@synthesize titleString;
@synthesize buttons;
//custom setters method this will handle all the buttons initilizations
- (void)setButtons:(NSArray *)inButtons
{
if(inButtons != buttons)
{
buttons = inButtons;
// [buttons release]; //ARC give's error in this part so comment it
// [inButtons retain];
if(buttons)
{
[self createButtonForThecell];//call your helper to add the buttons to cell
}
}
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
//put a label in the cell
UILabel *titleLabel = [[UILabel alloc]init];
titleLabel.tag = 1000;//to get it in the layout subviews
[self addSubview:titleLabel];//add it to cell
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)createButtonForThecell
{
//at this movement u get the buttons array contains the string (buttons) to be placed in the cell
int xValue,Yvalue;
xValue = 63;
Yvalue = 0;
int i = 1;
NSLog(@"buttons->%d",[self.buttons count]);
for(int k = 0 ;k< [self.buttons count];k++)//loop throug the button
{
//you need 3 for each row starts after 90 from x-axis
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(xValue , 2 + Yvalue, 65, 35)];
if(k == (3 * i) || (k % 3) == 2)
{
i = i + 1;
Yvalue += (35 + 5);
xValue = 63;
}
else
{
xValue += (63 + 5);
}
[button addTarget:self action:@selector(whenEventButtonClicked) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:[self.buttons objectAtIndex:k] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button setTag:k];//setting the tag to index it will become use for the acces of which button tapped
//finally add it to cell
[self addSubview:button];
}
}
- (void)whenEventButtonClicked
{
//hear put a delegate to controller for further processing of the event
NSLog(@"button clicked");
}
- (void)layoutSubviews
{
[super layoutSubviews];
//in the this always set the frme
UILabel *label = (UILabel *)[self viewWithTag:1000];
if(label)
{
label.frame = CGRectMake(2, 3, 55, 35);//your label in the left side
label.text = self.titleString;//set the string
label.textColor = [UIColor greenColor];//set the color for your label
}
}
@end
在控制器的.m文件中 我拿了一个包含如下词典的数组
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
present = NO;
listResultDataAry = [[NSMutableArray alloc]init];
NSMutableDictionary *tempDict = [[NSMutableDictionary alloc]init];
[tempDict setObject:@"Mrng" forKey:@"Date"];
[tempDict setObject:[[NSArray alloc]initWithObjects: @"Button1",@"Button2",@"Button3",@"button4",@"button5", nil] forKey:@"time"];
[listResultDataAry addObject:tempDict];
NSMutableDictionary *tempDict1 = [[NSMutableDictionary alloc]init];
[tempDict1 setObject:@"Eveng" forKey:@"Date"];
[tempDict1 setObject:[NSArray arrayWithObjects: @"Button1",@"Button2",@"Button3", nil] forKey:@"time"];
[listResultDataAry addObject:tempDict1];
NSMutableDictionary *tempDict2 = [[NSMutableDictionary alloc]init];
[tempDict2 setObject:@"Nyt" forKey:@"Date"];
[tempDict2 setObject:[[NSArray alloc]initWithObjects: @"Button1",@"Button2",@"Button3",@"Button4",@"button5",@"button6",@"button7",@"button 8", nil] forKey:@"time"];
[listResultDataAry addObject:tempDict2];
}
表视图中的委托方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;//return how many sections are there
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [listResultDataAry count];//return u hav for mrng,eveng,nyt for the more numbers u can resize the tableview cell
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if(cell == nil)
{
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
NSMutableDictionary *tempDict = [listResultDataAry objectAtIndex:indexPath.row];
NSLog(@"IndexPath.row = %d",indexPath.row);
cell.titleString = [tempDict objectForKey:@"Date"];//set the date for your label
cell.buttons = [tempDict objectForKey:@"time"];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//hear u need to cumpute the height for each cell
NSMutableDictionary *tempDict = [listResultDataAry objectAtIndex:indexPath.row];
NSArray *tempArray = [tempDict objectForKey:@"time"];
CGFloat height = [self computeHeight:tempArray];
return height;
}
- (CGFloat)computeHeight:(NSArray *)array
{
CGFloat height = 40; //0 1 2
int i = 1; //3 4 5
for(int k = 0; k< [array count];k++)
{
if(k == (3 * i))
{
height = height + 40;
i = i + 1;
}
}
return height;
}
希望这有助于你......:)
答案 1 :(得分:0)
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return [listResultDataAry count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section{
// 3 is morning , afternoon , night. But I think it will dynamic caculate to implement every label group.
return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//Calculate height by your dataSource
return PerLineHeight * ((BtnCount-1)/3+1)
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//Create a UILabel and set date by your dataSource
return dateLabel;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//Create a UILabel and set date by your dataSource
return dateLabel;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"ReuseCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier ];
if( cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
//init label for time
}
//Remove all buttons , set label value and add buttons by your datasource
return cell;
}