2013-03-28 73 views
0

我必须创建一个包含多个按钮的单元格。这里tableview是可扩展和折叠的,section的行数和行数是动态的。uitableviewcell中的多个按钮

我想以这种方式enter image description here

即每行,我想只有4个任务n还剩任务将显示在下一行的任务。但我的问题是,如果我只有13个任务比它包含4行,在第1第2和第3行它包含4个任务,并在最后一行它只包含1个任务,但即时通讯不能这样也它也不会改变为每任务。

这是我的代码。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if ([self tableView:tableView canCollapseSection:section]) 
    { 
     if ([expandedSections containsIndex:section]) 
     { 
      objePro = [appDelegate.array_proj objectAtIndex:section]; 

      rowcount = [objePro.arr_task count] + 1; 
      NSLog(@"rowcount %d",rowcount); 
      if (rowcount < 4) 
       return 2; 
      else 
      { 
       if (rowcount % 4) 
       { 
        NSLog(@"odd: %d",rowcount/4 + 2); 
        return rowcount/4 + 2; 
       } 
       else 
       { 
        NSLog(@"even: %d",rowcount/4); 
        return rowcount/4; 
       } 
      } 
     } 
    } 
    return 1; 
} 

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ; 
    } 
    cell.backgroundColor = [UIColor clearColor]; 

    if ([self tableView:tableView canCollapseSection:indexPath.section]) 
    { 
     if (!indexPath.row) 
     { 
      cell.textLabel.textColor = [UIColor whiteColor]; 
      UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:CGRectZero]; 
      cell.backgroundView = backgroundView; 
      backgroundView.image = [UIImage imageNamed:@"prjctcell_bg.png"]; 

      objePro = [appDelegate.array_proj objectAtIndex:indexPath.section]; 

      cell.textLabel.text = objePro.projctname; 
      if ([expandedSections containsIndex:indexPath.section]) 
       cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor blackColor] type:DTCustomColoredAccessoryTypeUp]; 
      else 
       cell.accessoryView = [DTCustomColoredAccessory accessoryWithColor:[UIColor blackColor] type:DTCustomColoredAccessoryTypeDown]; 
     } 
     else 
     { 
      // all other rows 
      cell.accessoryView = nil; 

      j=4; 

       for (k = 0; k<4; k++) 
       { 
        NSLog(@"k %d",k); 

        btnexpand = [[UIButton alloc] init]; 
        btnexpand = [UIButton buttonWithType:UIButtonTypeCustom]; 
        btnexpand.frame = CGRectMake(j, 5, 80, 40); 
        btnexpand.backgroundColor = [UIColor clearColor]; 
        [btnexpand setTitleColor:[UIColor colorWithRed:53.0/255 green:53.0/255 blue:53.0/255 alpha:1.0] forState:UIControlStateNormal]; 
        btnexpand.titleLabel.font = [UIFont boldSystemFontOfSize:14]; 

        [btnexpand setTitle:[NSString stringWithFormat:@"Step %d",[[[[appDelegate.array_proj objectAtIndex:indexPath.section] arr_task] objectAtIndex:indexPath.row - 1] taskid]] forState:UIControlStateNormal]; 
        NSLog(@"btn title: %@",btnexpand.titleLabel.text); 

        [btnexpand setBackgroundImage:[UIImage imageNamed:@"greenarrow.png"] forState:UIControlStateNormal]; 
        [cell.contentView addSubview:btnexpand]; 

        j= j+65; 
       } 

     } 
    } 
    return cell; 
} 

请帮帮我。

+0

@Manohar,我不想创建自定义单元格。如果没有自定义单元格,这件事情会发生吗 – Abha 2013-03-28 04:36:02

回答

1

numberOfRowsInSection

rowcount = [objePro.arr_task count]; // Keep original count 
if (rowcount % 4) // This means its not exact multiple of four 
    return rowcount/4 + 2; 
else 
    return rowcount + 1; 

在的cellForRowAtIndexPath

if(indexpath.row == 0){ 
    Your code for header line 
} 
else{ 
    objePro = [appDelegate.array_proj objectAtIndex:section]; 
    int rowcount = [objePro.arr_task count]; 
    if(rowcount < 4){ 
     for(int i = 0; i < 4; i++){ 
       NSLog (@"%d", i+1); 
     } 
    } 
    else{ 
     int iRow = rowcount/4; 
     if (rowcount % 4){ 
       for(int i = 0; i < 4; i++) 
        NSLog(@"Current Step %d", indexpath.row-1 + i); 
     } 
     else{ 
       for(int i = 0; i < rowcount % 4; i++) 
        NSLog(@"Current Step %d", indexpath.row-1 + i); 
     } 

    } 



} 

这是所有技巧,你需要把一些努力。希望我已经向你展示了一些方向。

+0

这里是什么iCount? – Abha 2013-03-28 04:43:36

+0

那是我:(我错了 – DivineDesert 2013-03-28 04:55:53