0

我正在尝试UITableView根据这些图像创建一个表格视图可以有多个部分及其标题。一个 Tableview 可以有多个部分和行数。但是,在每个部分中,只有一行可以选择。而且,稍后我可以在我UIAlertView的选定行中显示为消息和部分作为标题。我怎样才能做到这一点?

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"GroupCell"];

    if (cell == nil)
    {
        cell = [tableView dequeueReusableCellWithIdentifier:@"GroupCell"];
    }

    if (indexPath.row == self.selectedCell)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        cell.selected = YES;
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selected = NO;
    }

    cell.textLabel.text = @"mytext"
    cell.textLabel.textColor = [UIColor blackColor];

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.selectedCell = indexPath.row;

    [tableView reloadData]; // here i have to make the condition..
}

对此有任何想法吗?或者至少显示UITableView如下图就足够了..

在此处输入图像描述

4

5 回答 5

2

据我所知,您的部分数量是固定的。因此,当您选择一行时,请检查该部分内的行数,并使用UITableViewCellAccessoryTypenil 表示该行并accessoryType 选中选中行。一天结束时,您必须检查在每个部分中选择了哪些行,并使用该部分的数据将其关闭。

希望这可以帮助。

于 2013-07-25T06:26:56.057 回答
2

对于要从​​每个部分中选择的行,您必须保留一个selectedCell数组并且对于时间取消选择所有单元格,您必须管理另一个数组isFirsttimeSelection并为 rowCollapse 保留一个boolArray

接口或头文件实现..

NSArray *sectionArray;
NSMutableDictionary *dictionary;

NSMutableArray *boolArray;
NSMutableArray *selectedCell;
NSMutableArray *isFirsttimeSelection;

查看要使用的数组和字典的初始化。

dictionary=[[NSMutableDictionary alloc] initWithObjectsAndKeys:[NSArray arrayWithObjects:@"Cat",@"Dog",@"Lion",@"Tiger",@"Elephant",nil],@"Animals",[NSArray arrayWithObjects:@"Swallow",@"Parrot",@"Eagle",@"Owl",nil],@"Birds",[NSArray arrayWithObjects:@"Banana",@"Mango",@"Grapes", nil],@"Fruits",nil];

sectionArray=[[NSArray alloc] initWithArray:[dictionary allKeys]];

boolArray=[[NSMutableArray alloc] init];
selectedCell=[[NSMutableArray alloc] init];
isFirsttimeSelection=[[NSMutableArray alloc] init];

for (int i=0;i<sectionArray.count;i++) {
    [boolArray addObject:[NSNumber numberWithBool:NO]];
    [isFirsttimeSelection addObject:[NSNumber numberWithInt:NO]];
    [selectedCell addObject:[NSNumber numberWithInt:0]];
}

tableView 数据源和委托用作:---

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [sectionArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([boolArray[section] boolValue]) {
        return [[dictionary valueForKey:[sectionArray objectAtIndex:section]] count];
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier=@"cellIdentifier";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell==nil) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    cell.accessoryType=((indexPath.row == [selectedCell[indexPath.section] intValue]) && ([isFirsttimeSelection[indexPath.section] intValue]==1))?UITableViewCellAccessoryCheckmark:UITableViewCellAccessoryNone;

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text=[[dictionary valueForKey:[sectionArray objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 44;
}

部分的标题视图

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIButton *headerView=[UIButton buttonWithType:UIButtonTypeCustom];
    [headerView setTitle:[sectionArray objectAtIndex:section] forState:UIControlStateNormal];
    headerView.tag=section;
    headerView.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [headerView setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
    [headerView.titleLabel setFont:[UIFont boldSystemFontOfSize:16.0f]];
    [headerView setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [headerView setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [headerView.titleLabel setShadowOffset:CGSizeMake(0, -1)];

    [headerView addTarget:self action:@selector(sectionTouched:) forControlEvents:UIControlEventTouchUpInside];

    return  headerView;
}

单击标题视图上的操作

- (void)sectionTouched:(UIButton *)sender
{
    [boolArray replaceObjectAtIndex:sender.tag withObject:[NSNumber numberWithBool:([boolArray[sender.tag] boolValue])?NO:YES]];

    [_tblView reloadSections:[NSIndexSet indexSetWithIndex:sender.tag] withRowAnimation:([boolArray[sender.tag] boolValue])?UITableViewRowAnimationTop:UITableViewRowAnimationBottom];
}

最后确实选择了

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
     selectedCell[indexPath.section] = [NSString stringWithFormat:@"%i",indexPath.row];
    isFirsttimeSelection[indexPath.section]=[NSNumber numberWithBool:YES];
    [tableView reloadData];
}

下载它。

于 2013-07-25T09:41:57.830 回答
1

保留一个数组NSIndexpaths来处理选择了哪一行,当用户选择时检查节索引是否已经在数组中,如果存在则删除,并且可以在cellForRowAtIndexpath检查数组是否包含索引时管理选择,使单元格被选中accessoryType,并重新加载表when a new row selected and manage it properly

于 2013-07-25T06:19:34.243 回答
1

尝试这样的事情。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {
        UITableViewCell * selectedCell = [self.tableView cellForRowAtIndexPath:indexPath];
        if (indexPath.section == 1)
        {
            selectedCell.accessoryType = UITableViewCellAccessoryCheckmark;
            NSNumber * selectAnswer = [self.arrayWithObjects objectAtIndex:indexPath.row];
            if (![self.selectedAnswers containsObject:selectAnswer])
            {
                [self.arrSelObj addObject:selectAnswer];
            }
        }
    }

Where-self.ArrayWithObjects您的数组,其中包含方法中使用的元素numberOfRowsself.arrSelObj- 数组比包含你的答案。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
     if ([self.arrSelObj containsObject:[self.arrayWithObjects objectAtIndex:indexPath.row]])
            {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
}

在 didDeselect 中:

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * deselectedCell = [tableView cellForRowAtIndexPath:indexPath];    
    if (indexPath.section == 1)
    {

        deselectedCell.accessoryType = UITableViewCellAccessoryNone;

        NSNumber * deselectAnswer = [self.arrayWithObjects objectAtIndex:indexPath.row];
        [self.arrSelObj removeObject:deselectAnswer];
    }
}
于 2013-07-25T06:58:44.227 回答
0

您必须实现嵌套表,或者如果它的列表(价格...)是固定的,那么只需使用逻辑:) 就像点击特定按钮时的三个按钮然后设置表格框架然后设置剩余按钮。:)

于 2013-07-25T06:22:15.130 回答