简体   繁体   English

UITableView中的受限选择

[英]Limited Selection in UITableView

i have a tableview getting dynamic data from a service. 我有一个tableview从服务获取动态数据。 i want to select multiple rows limit is 3.. i have tried all the possible ways but can't get it out.. following is my code. 我想选择多行限制为3。我已经尝试了所有可能的方法,但无法解决..以下是我的代码。 Please help me out to solve this issue. 请帮我解决这个问题。 Thanks in Advance 提前致谢

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

}

There are two things you have to follow for this. 为此,您必须遵循两件事。 1.Since you want to select maximum of only three rows simultaneously,keep an array for tracking those row numbers and check whether that array has the row number in CellForRowAtIndexPath.But _WSConstCountrySelectedIndex has only the latest selected row number. 1.由于您只想同时选择最多三行,因此请保留一个数组以跟踪这些行号,并检查该数组在CellForRowAtIndexPath中是否具有行号。但是_WSConstCountrySelectedIndex仅具有最新选择的行号。

2.In didSelectRowAtIndex,update your array with the current selected row number.And make sure array does not exceed the count of 3.Similarly in didDeSelectRowAtIndex when the selected row is deselected then remove that row number in the array. 2.在didSelectRowAtIndex中,使用当前选定的行号更新数组。并确保数组不超过3的计数。类似地,当取消选择选定的行时,在didDeSelectRowAtIndex中删除该行号。

It must give you a start. 它必须给您一个开始。

In willSelectRowAtIndexPath check the count of number of rows selected. willSelectRowAtIndexPath检查所选行数。 If it is less than 3 than return index path or else return nil. 如果小于3,则返回索引路径,否则返回nil。

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

Hope this will help you. 希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM