Paul Heinemeyer Paul Heinemeyer - 1 year ago 103
Swift Question

How to get access to all cells at the Method didSelectItemAtIndexPath() in a UICollectionViewController (Swift)

how can set values of any cell in my collection view at didSelectItemAtIndexPath. So i want to change the border color of all other cells to black expect the cell the user clicked.

Thank you

Answer Source

The following code iterates through the tableview sections, and each sections's rows. It compares the indexPath of each row to the indexPath of the selected cell, and if they don't match, allows you to do what you need to do to the cell.

for (NSInteger i = 0; i < [tableView numberOfSections]; ++i)
    for (NSInteger j = 0; j < [tableView numberOfRowsInSection:i]; ++j)
        NSIndexPath *cellIdxPath = [NSIndexPath indexPathForRow:j inSection:i];

        // indexPath represents the currently selected row
        if (indexPath != cellIdxPath)
            UITableViewCell *cell = [tableView cellForRowAtIndexPath:cellIdxPath];
            // do whatever you need to do to the cell
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download