我已将UISegmentedController
添加到TableViewCell
中的CellForRowAtIndexPath
。
以下是代码。
[cell.segStatus addTarget:self action:@selector(selectedSegmentControl:) forControlEvents: UIControlEventValueChanged];
[cell.segStatus setSegmentedControlStyle:UISegmentedControlStyleBar];
cell.segStatus.tag = indexPath.row;
我将UITableViewCell
类用于自定义类。
我选择片段,当我滚动桌面视图时,其他单元格的分段控制器也会自动选择 为什么?我该怎么解决这个问题?
完整的CellForRowAtIndexPath方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
@try {
static NSString *defTableIdentifier = @"DefectRegisterCell";
DefectRegisterCell *cell = (DefectRegisterCell *)[self.tblDefectList dequeueReusableCellWithIdentifier:defTableIdentifier];
_defectEntry = [defectObjList objectAtIndex:indexPath.row];
cell.lblDefectNo.text = [NSString stringWithFormat:@"%@", _defectEntry.defectNo];
if ([_defectEntry.defect isEqual:@"None"] || [_defectEntry.defect isEqual:@""] ) {
cell.lblDefect.text = [NSString stringWithFormat:@"%@ - %@ - Passed", _defectEntry.item,_defectEntry.subItem];
}
else{
cell.lblDefect.text = [NSString stringWithFormat:@"%@ - %@ - %@", _defectEntry.item,_defectEntry.subItem,_defectEntry.defect];
}
cell.lblLocation.text = @"Location:";
cell.lblLocationValue.text = [NSString stringWithFormat:@"%@ > %@ > %@ > %@ (%@ - %@)",_defectEntry.building, _defectEntry.area, _defectEntry.location, _defectEntry.room,_defectEntry.orient, _defectEntry.position];
cell.lblAssignedTo.text = @"Assigned to:";
cell.lblAssignedToValue.text = _defectEntry.responsibleComp;
cell.lblDue.text = @"Due:";
cell.lblDueValue.text = _defectEntry.dueDate;
cell.lblCapturedBy.text = @"Recorded by:";
cell.lblCapturedByValue.text = [NSString stringWithFormat:@"%@ (%@)",_defectEntry.inspectedId,_defectEntry.inspectedCompId];
cell.lblDate.text = @"On:";
cell.lblDateValue.text = _defectEntry.createdDate;
NSString *attached = @"0" ;
NSString *fixClaimed = @"0";
NSString *completed = @"0";
if ([_defectEntry.attached length] > 0) {
attached = @"1";
}
if ([_defectEntry.fixClaimed isEqualToString:@"Y"]) {
fixClaimed = @"1";
}
if ([_common checkDateValidation:_defectEntry.completed]) {
completed = @"1";
}
[cell.segStatus addTarget:self action:@selector(selectedSegmentControl:) forControlEvents: UIControlEventValueChanged];
[cell.segStatus setSegmentedControlStyle:UISegmentedControlStyleBar];
cell.segStatus.tag = indexPath.row;
if ([attached isEqualToString:@"1"]) {
cell.imgAttachment.image = [UIImage imageNamed:@"diAttachments.png"];
}
if ([fixClaimed isEqualToString:@"1"]) {
cell.imgDefect.image = [UIImage imageNamed:@"diWarning.png"];
cell.segStatus.hidden = NO;
} else if ([completed isEqualToString:@"1"]) {
cell.imgDefect.image = [UIImage imageNamed:@"diRight.png"];
if ([_defectEntry.passed isEqualToString:@"Yes"]) {
cell.segStatus.hidden = YES;
}
else{
cell.segStatus.hidden = NO;
}
} else {
cell.imgDefect.image = [UIImage imageNamed:@"diWrong.png"];
cell.segStatus.hidden = NO;
}
UIView *backgroundView = [[UIView alloc] init];
if (indexPath.row % 2) {
backgroundView.backgroundColor = [UIColor whiteColor];
} else {
backgroundView.backgroundColor = [UIColor colorWithRed:242/255.0f green:242/255.0f blue:242/255.0f alpha:1.0f];
}
cell.backgroundView = backgroundView;
//change the selected cell color
UIView *selectedBackgroundView = [[UIView alloc] init];
selectedBackgroundView.backgroundColor = gaSelectedColor;
cell.selectedBackgroundView = selectedBackgroundView;
return cell;
}
@catch(NSException *excp)
{
NSLog(@"%@",excp);
}
}