UITableView中的有限选择

时间:2016-04-28 05:53:08

标签: ios objective-c uitableview ios6 xcode7

我有一个tableview从服务获取动态数据。我想选择多行限制是3 ..我已经尝试了所有可能的方法,但无法得到它..以下是我的代码。请帮我解决这个问题。在此先感谢

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [self.countriesArr count];
}


- (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.countriesArr objectAtIndex:row];


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

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

    }


    return cell;


}
#pragma mark -
#pragma mark Table view delegate

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


    [tableView deselectRowAtIndexPath:indexPath animated:YES];

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


    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];



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

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

        UITableViewCell *cell = [tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:_WSConstCountrySelectedIndex inSection:0]];


        if (cell != nil)
        {
            cell .accessoryType=UITableViewCellAccessoryNone;
        }

    }
    if( cell.accessoryType == UITableViewCellAccessoryCheckmark)
    {
        _WSConstCountryID=[thisRow objectForKey:_WSColumnCountryID];
        _WSConstCountryName=[thisRow objectForKey:_WSColumnCountryName];
        _WSConstCountrySelectedIndex=row ;
    }
    else
    {
        _WSConstCountryID=@"0";
        _WSConstCountryName=@"Select";
        _WSConstCountrySelectedIndex=-1 ;
    }

    [self.navigationController popViewControllerAnimated:YES];

}

2 个答案:

答案 0 :(得分:0)

你必须遵循两件事。 1.由于你想同时最多只选择三行,保留一个数组来跟踪那些行号并检查该数组是否在CellForRowAtIndexPath中有行号。但是_WSConstCountrySelectedIndex只有最新选择的行号。

2.在didSelectRowAtIndex中,使用当前选定的行号更新数组。并确保数组不超过3.在didDeSelectRowAtIndex中,当取消选择所选行时,删除数组中的该行号。

它必须给你一个开始。

答案 1 :(得分:0)

willSelectRowAtIndexPath中检查所选行数。如果它小于3而不是返回索引路径,否则返回nil。

- (nullable NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([tableView indexPathsForSelectedRows].count > 3) {
        return nil;
    }
    return indexPath;
}

希望这会对你有所帮助。