我是iOS的新手,我面临着根据数组计数启用和禁用单元格的问题
我的代码就像这样
countcellbtn.text=[NSString stringWithFormat:@"%lu",(unsigned long)ComplareArray.count];
我得到数组计数我需要根据数组计数禁用单元格。
确切问题:我已经按下了按钮按钮点击我正在保存数据 核心数据和弹出窗口我更新它。所以,如果我直接点击 第三排应用程序崩溃。
但是当我直接点击第3个单元格应用程序时会因为索引值得到更改而崩溃
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *STI=@"STI";
AuditNextTableViewCell *cell = (AuditNextTableViewCell *)[tableView dequeueReusableHeaderFooterViewWithIdentifier:STI];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"AuditNextTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.accessoryType=UITableViewCellAccessoryNone;
}
cell.audittitlelbl.text=[NSString stringWithFormat:@"%@",[idarray objectAtIndex:indexPath.row]];
cell.nameidlbl.text=[NSString stringWithFormat:@"%@",[CheckpointNameIDArray objectAtIndex:indexPath.row]];
cell.passlbl.text=[NSString stringWithFormat:@"%@",[Passarray objectAtIndex:indexPath.row]];
cell.faillbl.text=[NSString stringWithFormat:@"%@",[Failarray objectAtIndex:indexPath.row]];
cell.warninglbl.text=[NSString stringWithFormat:@"%@",[Warningarray objectAtIndex:indexPath.row]];
cell.nalbl.text=[NSString stringWithFormat:@"%@",[NAarray objectAtIndex:indexPath.row]];
cell.popuplbl.text=[NSString stringWithFormat:@"%@",[Popuparray objectAtIndex:indexPath.row ]];
// cell.remarklbl.text=[NSString stringWithFormat:@"%@",[PFWNAArray objectAtIndex:indexPath.row]];
DataModel *model2 = [AuditTextBoxarray objectAtIndex:indexPath.row];
cell.lblAuditTextBox.text=model2.AuditTextBoxString;
//cell.lblAuditTextBox.text=[NSString stringWithFormat:@"%@",[AuditTextBoxarray objectAtIndex:indexPath.row]];
cell.AuditCountlbl.text=[NSString stringWithFormat:@"%ld)", (long)indexPath.row +1];
cell.CaptureImage.image = [imageArray objectAtIndex:indexPath.row];
// tag add on buttons
cell.passbtn.tag = indexPath.row;
cell.failbtn.tag = indexPath.row;
cell.wipbtn.tag = indexPath.row;
cell.nabtn.tag = indexPath.row;
cell.Photobtn.tag=indexPath.row;
cell.btnRemove.tag=indexPath.row;
//method add on buttons
[cell.passbtn addTarget:self action:@selector(btnP:) forControlEvents:UIControlEventTouchUpInside];
[cell.failbtn addTarget:self action:@selector(btnF:) forControlEvents:UIControlEventTouchUpInside];
[cell.wipbtn addTarget:self action:@selector(btnW:) forControlEvents:UIControlEventTouchUpInside];
[cell.nabtn addTarget:self action:@selector(btnNA:) forControlEvents:UIControlEventTouchUpInside];
[cell.Photobtn addTarget:self action:@selector(btnphotoClick:) forControlEvents:UIControlEventTouchUpInside];
[cell.btnRemove addTarget:self action:@selector(btnRemoveClick:) forControlEvents:UIControlEventTouchUpInside];
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"AuditPost"];
self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
ComplareArray=[devices valueForKey:@"checkpointid"];
countcellbtn.text=[NSString stringWithFormat:@"%lu",(unsigned long)ComplareArray.count];
//cell.userInteractionEnabled = (ComplareArray.count % 2 == 0);
if (![arrIndexPaths containsObject:indexPath])
{
[arrIndexPaths addObject:indexPath];
}
if (![PassarrIndexPaths containsObject:indexPath])
{
[PassarrIndexPaths addObject:indexPath];
}
//background color change on button
DataModel *model = [arrData objectAtIndex:indexPath.row];
if([model.strSelected isEqualToString:@"P"])
{
cell.passbtn.backgroundColor = [UIColor redColor];
}
else if([model.strSelected isEqualToString:@"F"])
{
cell.failbtn.backgroundColor = [UIColor yellowColor];
}
else if([model.strSelected isEqualToString:@"W"])
{
cell.wipbtn.backgroundColor = [UIColor orangeColor];
}
else if([model.strSelected isEqualToString:@"NA"])
{
cell.nabtn.backgroundColor = [UIColor blueColor];
}
else if ([model.strSelected isEqualToString:@"PA"])
{
cell.passbtn.backgroundColor=[UIColor greenColor];
}
else if ([model.strSelected isEqualToString:@"NAA"])
{
cell.nabtn.backgroundColor=[UIColor blueColor];
}
else
{
cell.passbtn.backgroundColor = [UIColor lightGrayColor];
cell.failbtn.backgroundColor = [UIColor lightGrayColor];
cell.wipbtn.backgroundColor = [UIColor lightGrayColor];
cell.nabtn.backgroundColor = [UIColor lightGrayColor];
}
[cell.contentView.layer setBorderColor:[UIColor blackColor].CGColor];
[cell.contentView.layer setBorderWidth:0.5f];
return cell;
}
//buttons methods
-(void)btnP:(UIButton *)sender
{
sender.backgroundColor = [UIColor redColor];
DataModel *model = [arrData objectAtIndex:sender.tag];
model.strSelected = @"P";
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"AuditPost"];
self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
AuditNextTableViewCell *cell = sender.superview.superview;
NSIndexPath *indexPath = [Audittable indexPathForCell:cell];
CurrentIndexPath=indexPath.row;
NSString *String3=cell.audittitlelbl.text;
NSString *String =cell.actuallbl.text;
String2=cell.nameidlbl.text;
CGSize newSizeClient=CGSizeMake(200,200); // I am giving resolution 50*50 , you can change your need
UIGraphicsBeginImageContext(newSizeClient);
[cell.CaptureImage.image drawInRect:CGRectMake(0, 0, newSizeClient.width, newSizeClient.height)];
UIImage* newImageClient = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imgData = [[NSData alloc] initWithData:UIImageJPEGRepresentation((newImageClient), 0.5)];
AuditImageString = [imgData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
CheckStringShow =@"6";
NSLog(@"String =%@",String);
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
[defaults setObject:String forKey:@"ActualStringCustom"];
[defaults setObject:String2 forKey:@"CheckPointNameID"];
NSString *FailString=@"";
NSString *WarningString=@"";
HighString=@"";
UIAlertView *testAlert = [[UIAlertView alloc] initWithTitle:@"Enter High Risk Reason"
message:String3
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Done", nil];
textView1 = [UITextView new];
lbl = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 0.0,90.0, 34.0)];
[lbl setText:@"Enter Remark"];
[lbl setFont:[UIFont systemFontOfSize:12]];
[lbl setBackgroundColor:[UIColor clearColor]];
[lbl setTextColor:[UIColor lightGrayColor]];
textView1.delegate = self;
[textView1 addSubview:lbl];
[testAlert setValue: textView1 forKey:@"accessoryView"];
[testAlert show];
NSLog(@"Text View Data =%@",textView1.text);
NSManagedObjectContext *context = [self managedObjectContext];
NSError *error;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"AuditPost" inManagedObjectContext:context]];
// NSError *error = nil;
NSArray *results = [context executeFetchRequest:request error:&error];
NSLog(@"Result =%@",results);
int imageSize = imgData.length;
NSLog(@"size of image in KB: %d ", imageSize/1024);
int OCS=imageSize/1024;
NSLog(@"OCS Image Size =%d",OCS);
if(OCS<2)
{
AuditImageString=@"";
}
ComplareArray=[devices valueForKey:@"checkpointid"];
BOOL contains = [ComplareArray containsObject:String2];
if(contains == NO)
{
if (self.device) {
// Update existing device
[device setValue:Audit forKey:@"auditnameId"];
[device setValue:String forKey:@"checklistid"];
[device setValue:String2 forKey:@"checkpointid"];
[device setValue:FailString forKey:@"failreason"];
[device setValue:WarningString forKey:@"warningreason"];
[device setValue:HighString forKey:@"highregion"];
[device setValue:AuditStartDate forKey:@"starttimedate"];
[device setValue:userid forKey:@"userid"];
[device setValue:AuditImageString forKey:@"auditimage"];
NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
} else {
// Create a new device
NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"AuditPost" inManagedObjectContext:context];
[newDevice setValue:Audit forKey:@"auditnameId"];
[newDevice setValue:String forKey:@"checklistid"];
[newDevice setValue:String2 forKey:@"checkpointid"];
[newDevice setValue:FailString forKey:@"failreason"];
[newDevice setValue:WarningString forKey:@"warningreason"];
[newDevice setValue:HighString forKey:@"highregion"];
[newDevice setValue:AuditStartDate forKey:@"starttimedate"];
[newDevice setValue:userid forKey:@"userid"];
[newDevice setValue:AuditImageString forKey:@"auditimage"];
NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
}
}
else
{
NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"AuditPost" inManagedObjectContext:context]];
NSArray *results = [context executeFetchRequest:request error:&error];
NSManagedObject* favoritsGrabbed = [results objectAtIndex:CurrentIndexPath];
[favoritsGrabbed setValue:String forKey:@"checklistid"];
[favoritsGrabbed setValue:FailString forKey:@"failreason"];
[favoritsGrabbed setValue:WarningString forKey:@"warningreason"];
[favoritsGrabbed setValue:HighString forKey:@"highregion"];
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
}
//example reload table
dispatch_async(dispatch_get_main_queue(), ^{
[Audittable reloadData];
});
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
NSString *CheckString;
NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
CheckString=[defaults objectForKey:@"CheckStringCommentAudit"];
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"AuditPost"];
self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
NSLog(@"String 2=%@",String2);
NSManagedObjectContext *context = [self managedObjectContext];
compareCommentArray=[devices valueForKey:@"checkpointid"];
BOOL contains = [compareCommentArray containsObject:String2];
if(contains == YES)
{
if([CheckStringShow isEqualToString:@"4"])
{
NSLog(@"Text Value =%@",textView1.text);
NSString *String=textView1.text;
DataModel *model = [AuditTextBoxarray objectAtIndex:CurrentIndexPath];
model.AuditTextBoxString = String;
dispatch_async(dispatch_get_main_queue(), ^{
[Audittable reloadData];
});
NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
///
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"AuditPost" inManagedObjectContext:context]];
// NSError *error = nil;
NSArray *results = [context executeFetchRequest:request error:&error];
NSManagedObject* favoritsGrabbed = [results objectAtIndex:CurrentIndexPath];
[favoritsGrabbed setValue:String forKey:@"failreason"];
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
}
else if ([CheckStringShow isEqualToString:@"5"])
{
NSLog(@"Text Value =%@",textView1.text);
NSString *String=textView1.text;
DataModel *model = [AuditTextBoxarray objectAtIndex:CurrentIndexPath];
model.AuditTextBoxString = String;
dispatch_async(dispatch_get_main_queue(), ^{
[Audittable reloadData];
});
NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
///
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"AuditPost" inManagedObjectContext:context]];
// NSError *error = nil;
NSArray *results = [context executeFetchRequest:request error:&error];
NSManagedObject* favoritsGrabbed = [results objectAtIndex:CurrentIndexPath];
[favoritsGrabbed setValue:String forKey:@"warningreason"];
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
}
else if ([CheckStringShow isEqualToString:@"6"])
{
NSLog(@"Text Value =%@",textView1.text);
NSString *String=textView1.text;
DataModel *model = [AuditTextBoxarray objectAtIndex:CurrentIndexPath];
model.AuditTextBoxString = String;
dispatch_async(dispatch_get_main_queue(), ^{
[Audittable reloadData];
});
NSError *error = nil;
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
///
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"AuditPost" inManagedObjectContext:context]];
// NSError *error = nil;
NSArray *results = [context executeFetchRequest:request error:&error];
NSManagedObject* favoritsGrabbed = [results objectAtIndex:CurrentIndexPath];
[favoritsGrabbed setValue:String forKey:@"highregion"];
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
}
}
}