Ios 如何根据objective c中的数组计数启用和禁用tableview单元格
我是iOS新手,我面临着根据数组计数启用和禁用单元格的问题 我的代码是这样的Ios 如何根据objective c中的数组计数启用和禁用tableview单元格,ios,objective-c,arrays,uitableview,Ios,Objective C,Arrays,Uitableview,我是iOS新手,我面临着根据数组计数启用和禁用单元格的问题 我的代码是这样的 countcellbtn.text=[NSString stringWithFormat:@"%lu",(unsigned long)ComplareArray.count]; 我正在获取数组计数,我需要根据数组计数禁用单元格 确切的问题:我在按钮上点击了按钮,我正在保存数据 核心数据和弹出窗口我更新它。所以如果我直接点击 第三排应用程序崩溃 提前谢谢! 我在点击按钮时保存数据,在弹出式点击时更新数据,如下所示
countcellbtn.text=[NSString stringWithFormat:@"%lu",(unsigned long)ComplareArray.count];
我正在获取数组计数,我需要根据数组计数禁用单元格
确切的问题:我在按钮上点击了按钮,我正在保存数据
核心数据和弹出窗口我更新它。所以如果我直接点击
第三排应用程序崩溃
提前谢谢!
我在点击按钮时保存数据,在弹出式点击时更新数据,如下所示
但当我直接点击第三个手机应用程序时会崩溃,因为它的索引值会发生变化
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*STI=@“STI”;
AuditNextAttableViewCell*单元格=(AuditNextAttableViewCell*)[tableView dequeueReusableHeaderFooterViewWithIdentifier:STI];
如果(单元格==nil)
{
NSArray*nib=[[NSBundle mainBundle]loadNibNamed:@“AuditNextAttableViewCell”所有者:自选项:nil];
单元格=[nib objectAtIndex:0];
cell.accessoryType=UITableViewCellAccessoryNone;
}
cell.AuditTitleBL.text=[NSString stringWithFormat:@“%@,[idarray objectAtIndex:indexPath.row]];
cell.nameidbl.text=[NSString stringWithFormat:@“%@,[CheckpointNameIDArray objectAtIndex:indexPath.row]];
cell.passlbl.text=[NSString stringWithFormat:@“%@,[Passarray objectAtIndex:indexath.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.popullbl.text=[NSString stringWithFormat:@“%@,[Popuparray objectAtIndex:indexPath.row]];
//cell.remarklbl.text=[NSString stringWithFormat:@“%@,[PFWNAArray objectAtIndex:indexPath.row]];
DataModel*model2=[AuditTextBoxarray对象索引:indexPath.row];
cell.lblauditttextbox.text=model2.AuditTextBoxString;
//cell.lblauditttextbox.text=[NSString stringWithFormat:@“%@,[AuditTextBoxarray objectAtIndex:indexPath.row]];
cell.AuditCountlbl.text=[NSString stringWithFormat:@“%ld)”,(长)indexath.row+1];
cell.CaptureImage.image=[imageArray objectAtIndex:indexPath.row];
//标记附加按钮
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;
//方法附加按钮
[cell.passbtn addTarget:self action:@selector(btnP:)forControlEvents:UIControlEventTouchUpInside];
[cell.failbtn addTarget:self action:@selector(btnF:)for controlEvents:UIControlEventTouchUpInside];
[cell.wipbtn addTarget:self action:@selector(btnW:)forControlEvents:UIControlEventTouchUpInside];
[cell.nabtn addTarget:self action:@selector(btnNA:)for controlEvents:UIControlEventTouchUpInside];
[cell.Photobtn addTarget:self action:@selector(btnphotoClick:)for controlEvents:UIControlEventTouchUpInside];
[cell.btnRemove addTarget:self action:@selector(btnRemoveClick:)for ControlEvents:UIControlEventTouchUpInside];
NSManagedObjectContext*managedObjectContext=[self-managedObjectContext];
NSFetchRequest*fetchRequest=[[NSFetchRequest alloc]initWithEntityName:@“AuditPost”];
self.devices=[[managedObjectContext executeFetchRequest:fetchRequest错误:nil]mutableCopy];
平面阵列=[devices valueForKey:@“checkpointid”];
countcellbtn.text=[NSString stringWithFormat:@“%lu”,(无符号长)SUPPREARAY.count];
//cell.userInteractionEnabled=(complaurearray.count%2==0);
if(![arrindxpath包含对象:indexPath])
{
[arrIndexPaths addObject:indexPath];
}
if(![PassarrIndexPaths containsObject:indexPath])
{
[passarindexpaths addObject:indexPath];
}
//按钮上的背景颜色更改
DataModel*model=[arrdataobjectatindex:indexath.row];
如果([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];
}
其他的
{
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];
返回单元;
}
//按钮方法
-(无效)btnP:(UIButton*)发送方
{
sender.backgroundColor=[UIColor redColor];
DataModel*model=[arrdataobjectatindex:sender.tag];
model.strSelected=@“P”;
NSManagedObjectContext*managedObjectContext=[self-managedObjectContext];
NSFetchRequest*fetchRequest=[[NSFetchRequest alloc]initWithEntityName:@“AuditPost”];
self.devices=[[managedObjectContext executeFetchRequest:fetchRequest错误:nil]mutableCopy];
AuditNextAttableViewCell*单元格=sender.superview.superview;
NSIndexPath*indexPath=[Audittable indexPathForCell:cell];
当前索引
- (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]);
}
}
}
}