Search code examples
objective-cuitableviewcellselectionrepeat

selection of cell is repeating while loading in tableview in objective c


Tick mark button selection of uitableview cell is repeating when scroll the tableview. and the tableview is already loaded with an array of item. but i need to do multi selection of cells in tableview

-(void)checkButtonClicked:(UIButton *)sender{

CGPoint touchPoint = [sender convertPoint:CGPointZero toView:_tableView];
  NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:touchPoint];
    sender.tag = indexPath.row;
    selectedIndexForPassing = sender.tag;
    if([_selectionArray count ] == 0){
  _uploadSheetBtn.enabled = NO;
if (sender.selected){
    sender.selected = false;

}
else{
    TCSTimeSheet * sc = self.timesheetList[selectedIndexForPassing];
    _canEdit = sc.canEdit;
    if([_canEdit  isEqual: @"1"]){
    sender.selected = true;
    _uploadSheetBtn.enabled = YES;


    TCSTimeSheet * sc = self.timesheetList[selectedIndexForPassing];

    NSDictionary * detailsDictionary= [NSDictionary dictionaryWithObjectsAndKeys:sc.timesheetDetailId,@"timesheetDetailId",sc.scheduleEmployeeDetailId, @"scheduleDetailId",sc.lastUpdatedTime,@"lastUpdatedTime",sc.timesheetStatus,@"timesheetStatus",sc.shiftDate , @"shiftDate",sc.employeeId ,@"employeeId",nil];

    [_storingArray addObject:detailsDictionary];

    [_selectionArray addObject:sc.clientId];
    }

}

}

here is my cell for at indexpath code

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

   static NSString *  reuseIdentifier = @"timeSheetCell" ;


        TimeSheetCell * dataCell =  [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];

        TCSTimeSheet * currentTimeSheet =  self.timesheetList[indexPath.row] ;
        _statusIndicator = currentTimeSheet.timesheetStatus;
        _timesheetDetailId = currentTimeSheet.timesheetDetailId;
        _timesheetDocId = currentTimeSheet.timesheetDocId;
        _documentType = currentTimeSheet.documentType;


        if(![currentTimeSheet.notes isEqual:@""]){
            _notes = currentTimeSheet.notes;
        }
        if (dataCell == nil)
           {
               dataCell = [[TimeSheetCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];

           }

            dataCell.clientNameLbl.text = currentTimeSheet.clientName;
            dataCell.dayLbl.text = currentTimeSheet.shiftDay;
            dataCell.dateLbl.text = currentTimeSheet.shiftDate;
            dataCell.shiftTime.text = currentTimeSheet.shiftTime;
            dataCell.shiftType.text = currentTimeSheet.shiftType;


        [dataCell.checkBtn addTarget:self action:@selector(checkButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

       if ([_statusIndicator  isEqual: @"D"]) {

           if([currentTimeSheet.canEdit isEqual:@"1"]) {

           [dataCell.remarkBtn setImage:[UIImage imageNamed:@"selectedMessage_btn"] forState:UIControlStateNormal];
           dataCell.containerView.backgroundColor = [UIColor colorWithDisplayP3Red:255.0 green:191.0 blue:0 alpha:0.5];

               if(![currentTimeSheet.notes isEqual:@""]){

                [dataCell.remarkBtn addTarget:self action:@selector(presentMessagePopUp:) forControlEvents:UIControlEventTouchUpInside];
               }

           }
       }
       else if ([_statusIndicator  isEqual: @"U"]){

           if([currentTimeSheet.canDelete isEqual:@"1"]){
           [dataCell.remarkBtn setImage:[UIImage imageNamed:@"selectedDelete_btn"] forState:UIControlStateNormal];
            [dataCell.remarkBtn addTarget:self action:@selector(deleteDocument:) forControlEvents:UIControlEventTouchUpInside];
           }



       }
       else{

           [dataCell.remarkBtn setImage:[UIImage imageNamed:@"unselectedDelete_btn"] forState:UIControlStateNormal];
           dataCell.containerView.backgroundColor = [UIColor colorWithDisplayP3Red:255 green:255 blue:255 alpha:1];
           [dataCell.checkBtn setImage:[UIImage imageNamed:@"timesheetUnselectedTick"] forState:UIControlStateNormal];

       }

    if([_timesheetDocId isEqual: @""] && [_timesheetDetailId isEqual:@""]) {
        [dataCell.previewBtn setImage:[UIImage imageNamed:@"unselectedView_btn"] forState:UIControlStateNormal];

    }
    else {
        [dataCell.previewBtn setImage:[UIImage imageNamed:@"selectedView_btn"] forState:UIControlStateNormal];


            if([_documentType isEqual:@"image"]){

            [dataCell.previewBtn addTarget:self action:@selector(buttonClickedToShowImage:) forControlEvents:UIControlEventTouchUpInside];
            }
            else {

            [dataCell.previewBtn addTarget:self action:@selector(buttonClickedToShowDocuments:) forControlEvents:UIControlEventTouchUpInside];
            }



    }



        return dataCell ;

    }



**button action of the tick mark**

-(void)checkButtonClicked:(UIButton *)sender{

CGPoint touchPoint = [sender convertPoint:CGPointZero toView:_tableView];
  NSIndexPath *indexPath = [_tableView indexPathForRowAtPoint:touchPoint];
    sender.tag = indexPath.row;
    selectedIndexForPassing = sender.tag;
    if([_selectionArray count ] == 0){
  _uploadSheetBtn.enabled = NO;
if (sender.selected){
    sender.selected = false;

}
else{
    TCSTimeSheet * sc = self.timesheetList[selectedIndexForPassing];
    _canEdit = sc.canEdit;
    if([_canEdit  isEqual: @"1"]){
    sender.selected = true;
    _uploadSheetBtn.enabled = YES;


    TCSTimeSheet * sc = self.timesheetList[selectedIndexForPassing];

    NSDictionary * detailsDictionary= [NSDictionary dictionaryWithObjectsAndKeys:sc.timesheetDetailId,@"timesheetDetailId",sc.scheduleEmployeeDetailId, @"scheduleDetailId",sc.lastUpdatedTime,@"lastUpdatedTime",sc.timesheetStatus,@"timesheetStatus",sc.shiftDate , @"shiftDate",sc.employeeId ,@"employeeId",nil];

    [_storingArray addObject:detailsDictionary];

    [_selectionArray addObject:sc.clientId];
    }

}

}
else{
    TCSTimeSheet * sc = self.timesheetList[selectedIndexForPassing];
     if (sender.selected){
         NSDictionary * detailsDictionary= [NSDictionary dictionaryWithObjectsAndKeys:sc.timesheetDetailId,@"timesheetDetailId",sc.scheduleEmployeeDetailId, @"scheduleDetailId",sc.lastUpdatedTime,@"lastUpdatedTime",sc.timesheetStatus,@"timesheetStatus",sc.shiftDate,@"shiftDate",sc.employeeId,@"employeeId",nil];
            sender.selected = false;
           [_storingArray removeObject:detailsDictionary];
           //[_selectionArray removeAllObjects];
         if([_storingArray count] == 0){
             _uploadSheetBtn.enabled = NO;
            [_selectionArray removeAllObjects];

         }

       }
     else{
    if([_selectionArray firstObject] == sc.clientId){
        _uploadSheetBtn.enabled = YES;
        sender.selected = true;

        NSDictionary * detailsDictionary= [NSDictionary dictionaryWithObjectsAndKeys:sc.timesheetDetailId,@"timesheetDetailId",sc.scheduleEmployeeDetailId, @"scheduleDetailId",sc.lastUpdatedTime,@"lastUpdatedTime",sc.timesheetStatus,@"timesheetStatus",sc.shiftDate , @"shiftDate",sc.employeeId ,@"employeeId",nil];

        [_storingArray addObject:detailsDictionary];

        }
    else{
        sender.selected = false;
        UIAlertView *alertCantDisply=[[UIAlertView alloc]initWithTitle:@"Can't Select" message:@"Can choose same client only" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alertCantDisply show];

        }
     }



}

}


Solution

  • The cell will reuse, but the model will not, and the state of the view should be recorded in the model