【问题标题】:How to handle multiple row selection of data coming from service如何处理来自服务的数据的多行选择
【发布时间】:2016-06-01 10:41:39
【问题描述】:

我想处理多行选择,即不超过 3 行,数据来自服务(URL)。下面是我尝试过的部分代码,但无法处理多行选择。你能不能花点时间来解决它。 TIA

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSUInteger row = [indexPath row];
    static NSString *MyIdentifier = @"tableCell";
    CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

    if (cell == nil) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];

        for (id currentObject in topLevelObjects){
            if ([currentObject isKindOfClass:[UITableViewCell class]]){
                cell =  (CustomCell *) currentObject;
            }
        }
    }

    NSDictionary *thisRow = [self.professionArr objectAtIndex:row];

    if(_WSConstProfessionID !=nil && ![_WSConstProfessionID isEqual:@"0"] && ![_WSConstProfessionID isEqual:@""] && _WSConstProfessionSelectedIndex ==row  ){
        cell .accessoryType=UITableViewCellAccessoryCheckmark;
    } else {
        cell .accessoryType=UITableViewCellAccessoryNone;
    }

    cell.lblTitle.text = [thisRow objectForKey:_WSColumnProfessionName];
    NSString *str=[thisRow objectForKey:_WSColumnProfessionID];
    NSString *stra=_WSConstProfessionID;
    if ([str isEqualToString:stra]) {
        cell .accessoryType=UITableViewCellAccessoryCheckmark;
        cell.highlighted=YES;
    } else {
        cell .accessoryType=UITableViewCellAccessoryNone;
    }

    return cell;
}

#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    NSUInteger row = [indexPath row];
    NSDictionary *thisRow=[self.professionArr  objectAtIndex:row];
    NSLog(@"%@",[thisRow description]);

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if (cell.accessoryType == UITableViewCellAccessoryNone){
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }


    if(_WSConstProfessionID!=nil && ![_WSConstProfessionID isEqual:@"0"] && ![_WSConstProfessionID isEqual:@""] &&_WSConstProfessionSelectedIndex!=row){

        UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_WSConstProfessionSelectedIndex inSection:0]];
        if (cell != nil){
            cell .accessoryType=UITableViewCellAccessoryNone;
        }
    }

    if( cell.accessoryType == UITableViewCellAccessoryCheckmark){
        _WSConstProfessionID=[thisRow objectForKey:_WSColumnProfessionID];
        _WSConstProfessionName=[thisRow objectForKey:_WSColumnProfessionName];
        _WSConstProfessionSelectedIndex=row ;
    } else {
        _WSConstProfessionID=@"0";
        _WSConstProfessionName=@"Select";
        _WSConstProfessionSelectedIndex=-1 ;
    }

    [self.navigationController popViewControllerAnimated:YES];
}

【问题讨论】:

  • 你能显示professionalArr值吗?并且您希望用户最多可以选择 3 行。

标签: ios objective-c iphone xcode uitableview


【解决方案1】:

请在 viewdidload 方法中设置这一行

   tableView.allowsMultipleSelectionDuringEditing = true

添加此行后应用以下代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

[tableView deselectRowAtIndexPath:indexPath animated:YES];

NSUInteger row = [indexPath row];
NSDictionary *thisRow=[self.professionArr  objectAtIndex:row];
NSLog(@"%@",[thisRow description]);

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

if (cell.accessoryType == UITableViewCellAccessoryNone){
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}


if(_WSConstProfessionID!=nil && ![_WSConstProfessionID isEqual:@"0"] && ![_WSConstProfessionID isEqual:@""] &&_WSConstProfessionSelectedIndex!=row){

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_WSConstProfessionSelectedIndex inSection:0]];
    if (cell != nil){
        cell .accessoryType=UITableViewCellAccessoryNone;
    }
}

if( cell.accessoryType == UITableViewCellAccessoryCheckmark){
    _WSConstProfessionID=[thisRow objectForKey:_WSColumnProfessionID];
    _WSConstProfessionName=[thisRow objectForKey:_WSColumnProfessionName];
    _WSConstProfessionSelectedIndex=row ;
} else {
    _WSConstProfessionID=@"0";
    _WSConstProfessionName=@"Select";
    _WSConstProfessionSelectedIndex=-1 ;
}

[self.navigationController popViewControllerAnimated:YES];
}

【讨论】:

    【解决方案2】:

    请使用此代码 听我说NSMutableArray 数组名categoryArray

    将此数组分配到viewdidload()

    categoryArray = [NSMutableArray array];
    

    categoryArray 中添加_WSColumnProfessionName id 并处理代码。

      -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
        {
            @try {
    
    
                NSUInteger row = [indexPath row];
                static NSString *MyIdentifier = @"tableCell";
                CustomCell *cell = (CustomCell *) [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    
                if (cell == nil) {
                    NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
    
                    for (id currentObject in topLevelObjects){
                        if ([currentObject isKindOfClass:[UITableViewCell class]]){
                            cell =  (CustomCell *) currentObject;
                        }
                    }
                }
    
                cell.lblTitle.text = [thisRow objectForKey:_WSColumnProfessionName];
                if (categoryArray.count == 0)
                {
                    NSLog(@"category array is empty.");
                }
                else
                {
                    NSString *s = [NSString stringWithFormat:@"%@",[[save_cat_array objectAtIndex:indexPath.row] valueForKey:@"category_id"]];
                    for (int p =0 ; p<categoryArray.count; p++)
                    {
    
                        NSString *b =[NSString stringWithFormat:@"%@",[categoryArray objectAtIndex:p]];
                        if ([s isEqualToString:b])
                        {
                            cell.accessoryType = UITableViewCellAccessoryCheckmark;
                            cell.lblTitle.textt = [[self.professionArr objectAtIndex:indexPath.row]valueForKey:_WSColumnProfessionName];
                            break;
                        }
                        else
                        {
                            cell.lblTitle.text = [[self.professionArr objectAtIndex:indexPath.row]valueForKey:_WSColumnProfessionName];
                            cell.accessoryType = UITableViewCellAccessoryNone;
                        }
                    }
    
                }
    
                 return cell;
            }
            @catch (NSException *exception) {
    
            }
        }
    
        -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
        {
            @try {
    
    
                [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
                UITableViewCell  *cell = ( UITableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
    
                if (cell.accessoryType == UITableViewCellAccessoryNone)
                {
                    [categoryArray addObject:[[self.professionArr objectAtIndex:indexPath.row] valueForKey:_WSColumnProfessionID];
                    cell.accessoryType = UITableViewCellAccessoryCheckmark;
                }
                else
                {
    
                    cell.accessoryType = UITableViewCellAccessoryNone;
                    NSString *a = [NSString stringWithFormat:@"%@",[self.professionArr objectAtIndex:indexPath.row] valueForKey:_WSColumnProfessionID]];
                    for (int i=0;i<categoryArray.count;i++)
                    {
                        if ([a isEqualToString:[NSString stringWithFormat:@"%@",[categoryArray objectAtIndex:i]]])
                        {
                            [categoryArray removeObjectAtIndex:i];
                        }
                    }
    
                }
    
            }
            @catch (NSException *exception) {
    
            }
        }
    

    您还可以根据 categoryArray.count == 3 处理最多 3 个条件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-03
      • 1970-01-01
      • 2012-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-09
      • 1970-01-01
      相关资源
      最近更新 更多