Domanda

I want to implement like this customized tableview.

Have any one can teach me how to implement this tableview method?

The "morning" , "afternoon", and "night" label have to put left.

Then there are many different button after the label.

If a row have over 3 buttons. It will put next line(row)(like below photo "afternoon" ).

I understand the date is use header title. Then Morning, afternoon, night block used section.

But I don't know how to implement the "morning", "afternoon" , "night" label put left, then the button put right. And the key problem is I can't set afternoon(morning or night) group.

Have any one can give me some hint , or some tutorial how to dynamic set the (afternoon, morning or night) group?

Thank you very much !!!!! <(_ _)>

dynamic table view

    #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;
            }
È stato utile?

Soluzione

sorry for late answer but u don't need to use the different row if the button count exceeds more than 3, it will be complicated management of tableview, instead why not you use the sing row with different height with all the buttons can fit at a proper place inside the tableview cell.

look at the code below u can try it in the separate project, for the custom cell don't use the default cell that is "UITableViewCell" instead subclass it, there by u can manage the cell contents easily (i suggest)

in the subclassed cell .m file (i took one string for date and the array contains the button title) that is your time


  #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

in .m file

 #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

in .m file of controller i took one array which contains dictionaries like below

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

  }

in table view delegate methods

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

hope this helps u ... :)

Altri suggerimenti

-(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;
} 
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top