我必須創建一個包含多個按鈕的單元格。這裏tableview是可擴展和摺疊的,section的行數和行數是動態的。uitableviewcell中的多個按鈕
我想以這種方式
即每行,我想只有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;
}
請幫幫我。
@Manohar,我不想創建自定義單元格。如果沒有自定義單元格,這件事情會發生嗎 – Abha 2013-03-28 04:36:02