Need to create a select all button for UITableView and add selections to an array in iOS

后端 未结 3 573
时光取名叫无心
时光取名叫无心 2021-01-23 16:49

I have a UITableViewController that shows a list of items from NSMutableArray, along with a button that serves as a check box that the user can select/deselect. I am able to su

3条回答
  •  青春惊慌失措
    2021-01-23 17:36

    Take another NSMUtableArray as SelectedArray

    in didselectRowAtIndexPath row You can Add remove objects from SelectedArray.

    You can select a cell calling table view's selectRowAtIndexPath method:

    [yourtable selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
    

    Put for loop for selectedArray to putcheckbutton only in selected cells.

    in Your CellForRow Method Use this

    testButton.tag=indexpath.row;
    

    you can select All row and All Section by below method.

    for (int i = 0; i < [ptableView numberOfSections]; i++) {
        for (int j = 0; j < [ptableView numberOfRowsInSection:i]; j++) {
            NSUInteger ints[2] = {i,j};
            NSIndexPath *indexPath = [NSIndexPath indexPathWithIndexes:ints length:2];
                UITableViewCell *cell = [ptableView cellForRowAtIndexPath:indexPath];
               //Here is your code
    
          UiButton *btn = (UiButton*)[cell.contentView viewWithTag:j]
    
    if( [[btn imageForState:UIControlStateNormal] isEqual:[UIImage imageNamed:@"CheckBox1.png"]])
        {
            [btn setImage:[UIImage imageNamed:@"CheckBox2.png"] forState:UIControlStateNormal];
            // other statements
        }
        else
        {
            [btn setImage:[UIImage imageNamed:@"CheckBox1.png"] forState:UIControlStateNormal];
            // other statements
        }
            }
        }
    

提交回复
热议问题