如何处理来自服务

时间:2016-06-01 10:41:40

标签: ios objective-c iphone xcode uitableview

我想处理来自服务(URL)的数据的多行选择,即不超过3行。下面是我尝试过的代码的一部分,但无法处理多行选择。你能不花一些时间来解决它。 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];
}

2 个答案:

答案 0 :(得分:0)

请在viewdidload方法中设置此行

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

添加此行后,请应用以下代码

nocache

答案 1 :(得分:0)

使用此代码 我听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条件