0

In tableview selcting a row in didselectrow i used this

iPhone :UITableView CellAccessory Checkmark

by selecting one row i'm callling a webservice

PROBLEM : i want only one row selection not another by next time what to do stop another rows to being selected

Community
  • 1
  • 1

1 Answers1

0

Add tableView.allowsSelection = false to didSelectRowAtIndexPath: and then re-set it to true at the appropriate time (I'm guessing after your web service stuff completes).

ADDED:

To make it so that the selected row cannot be selected again, I would add

Swift:

var selectedRows = [Int]()

as an instance variable (i.e. at the class level, not within a method).

Then I would change didSelectRowAtIndexPath: to something like this:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if !(selectedRows as NSArray).containsObject(indexPath.row) {
        // Request data from web service because this row has not been selected before
        selectedRows.append(indexPath.row) // Add indexPath.row to the selectedRows so that next time it is selected your don't request data from web service again
        let cell = tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell
        cell.selectionStyle = UITableViewCellSelectionStyle.None
    }
}

Objective-C:

@property (strong, nonatomic) NSMutableArray *selectedRows;

In the view controller, initialize selectedRows:

- (NSMutableArray *)selectedRows
{
    if (!_selectedRows) {
        _selectedRows = [[NSMutableArray alloc] init];
    }
    return _selectedRows;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (![self.selectedRows containsObject:indexPath.row]) {
        // Request data from web service because this row has not been selected before
        [self.selectedRows addObject:indexPath.row]; // Add indexPath.row to the selectedRows so that next time it is selected your don't request data from web service again
        UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
}
trevorj
  • 2,029
  • 1
  • 16
  • 11
  • nice answer but another question i have if i checked and send call to webservice then how to stop selecting it again – user3595019 Dec 31 '14 at 09:36
  • Maybe I didn't understand your question, but every time `didSelectRowAtIndexPath:` is called, `tableView.allowsSelection` will be set to `false`. I assume that you don't want the user to select anything while the web service is fetching the data, right? so only set `allowsSelection` to `true` after you get your data from the web service in whatever method receives the data from the web server. – trevorj Dec 31 '14 at 09:42
  • yes u didn't understood my question i'm selecting a row and on selecting i'm calling webservice which sends data to server Problem: how to stop selecting already selected row in did select row – user3595019 Dec 31 '14 at 09:45
  • I see what you're asking. So after a cell is selected, it should no longer be selectable, right? I would create an instance variable like `var selectedRows = [Int]()` (at the class level) and then in `didSelectRowAtIndexPath:` I would store the `indexPath.row` of the selected row in the `selectedRows` array with `selectedRows.append(indexPath.row)`. Then, in `didSelectRowAtIndexPath:` I would check to make sure that the selected cell is not in the instance variable `selectedRows` by doing something like `if !(selectedRows as NSArray).contains(indexPath.row)`. – trevorj Dec 31 '14 at 10:41
  • If the selected row's `indexPath.row` is not in the array, that means it hasn't been selected before, so you'd want to run your networking code. – trevorj Dec 31 '14 at 10:42
  • what is var means variable or array – user3595019 Dec 31 '14 at 10:52
  • `var` indicates a variable, but the `[Int]()` creates and tells the compiler that this is an `Array` variable that holds `Int`s. – trevorj Dec 31 '14 at 10:54
  • No, defining `var selectedRows = [Int]()` at the class level (i.e. not in a method) in a view controller cannot generate an error. – trevorj Dec 31 '14 at 11:00
  • please can u post another answer on it i'm getting use of undeclared variable var – user3595019 Dec 31 '14 at 11:03
  • i'm using objective c not swift yaar sorry for not mentioning that – user3595019 Dec 31 '14 at 11:19
  • Added the Objective-C equivalent. Hope it helps and best of luck! – trevorj Dec 31 '14 at 11:54
  • @trevroj tnx for ur reply but if (![self.selectedRows containsObject:indexPath.row]) line thorws error – user3595019 Dec 31 '14 at 12:46
  • You need to initialize `self.selectedRows`. I updated the code. That should help. – trevorj Dec 31 '14 at 12:53