简体   繁体   English

从URL限制UITableView中的行选择

[英]Limited Selection of rows in UITableView from a URL

The following code is for getting the data from a URL. 以下代码用于从URL获取数据。 I want to select a limited of 3 rows selection.. Plz help me in solving this problem. 我想选择一个有限的3行选择。.Plz帮助我解决了这个问题。 TIA TIA

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    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];

}

You can have a global variable and set its value to 0 initially, Increase/Decrease its value in did select and in did deselect. 您可以有一个全局变量,并将其值初始设置为0,在“选择”和“取消选择”中增加/减小其值。 Make sure value should be within 0-3 and show appropriate message when the value is trying to cross 3. 确保值应在0-3之内,并在尝试越过3时显示适当的消息。

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

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