Change border color of a UITableViewCell on selection

后端 未结 3 455
时光取名叫无心
时光取名叫无心 2020-12-28 17:45

I am using a custom table view cell for my tableview. For setting the border, I have put a view on the custom cell and I am changing its border properties.

s         


        
3条回答
  •  甜味超标
    2020-12-28 18:04

    Will have to mark/un-mark(assuming u need only one selected at a time) the border color again and again like-

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        if(indexPath.row==self.selectedRow){
    cell.borderView.layer.borderColor = [UIColor yellowColor].CGColor;
    }else {
    cell.borderView.layer.borderColor = [UIColor clearColor].CGColor;
    }
    }
    

    Just save/cache the selected index like-

        - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //Unselected the prevoius selected Cell
                YourCellClass *aPreviousSelectedCell=  (YourCellClass*)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:self.selectedRow inSection:0]];
                aPreviousSelectedCell.borderView.layer.borderColor = [UIColor clearColor].CGColor;
    
    //Selected the new one-    
               YourCellClass *aSelectedCell = (YourCellClass*)[tableView cellForRowAtIndexPath:indexPath];
    
        aSelectedCell.borderView.layer.borderColor = [UIColor yellowColor].CGColor; 
    
                self.selectedRow = indexPath.row;
            }
    

提交回复
热议问题