我是iOS新手,我遇到了启用和禁用UITableView单元的问题。我想启用第一个单元格并禁用其他单元格。当我单击第一个单元格时,第二个单元格将启用。我可以这样做吗?
我正在使用此代码:
cell.userInteractionEnabled = false;
但是它会禁用表格视图中的所有单元格。我可以逐个启用电池吗?
我没有点击单元格,而是单元格中的一个按钮,它运行此方法:
- (void)rateView:(RateView *)rateView ratingDidChange:(int)rating
{
NSLog(@"Rating =%d",rating);
rateviewintvalue=rating;
KPINextTableview *cell = rateView.superview.superview;
// NSLog(@"%@",cell.actuallbl.text);
NSIndexPath *indexPath = [kpitable indexPathForCell:cell];
CurrentIndexPath=indexPath.row;
String2=cell.Kpiidlbl.text;
self.currentlyEnabledCellIndex = self.currentlyEnabledCellIndex + 1;
NSLog(@"String 2 =%@",String2);
NSString *RateString=[NSString stringWithFormat:@"%d",rating];
ratingcoredata=RateString;
kpiidcoredata=String2;
commentString=@"";
NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"Get_KpiPoints_User"];
self.devices = [[managedObjectContext executeFetchRequest:fetchRequest error:nil] mutableCopy];
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);
CompareArray=[devices valueForKey:@"kpiid"];
BOOL contains = [CompareArray containsObject:kpiidcoredata];
if(contains == NO)
{
if (self.device) {
// Update existing device
[self.device setValue:ratingcoredata forKey:@"rating"];
[self.device setValue:kpiidcoredata forKey:@"kpiid"];
[self.device setValue:commentString forKey:@"kpicomment"];
} else {
// Create a new device
NSManagedObject *newDevice = [NSEntityDescription insertNewObjectForEntityForName:@"Get_KpiPoints_User" inManagedObjectContext:context];
// NSLog(@"context",newDevice);
[newDevice setValue:ratingcoredata forKey:@"rating"];
[newDevice setValue:kpiidcoredata forKey:@"kpiid"];
[newDevice setValue:commentString forKey:@"kpicomment"];
//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:@"Get_KpiPoints_User" inManagedObjectContext:context]];
// NSError *error = nil;
NSArray *results = [context executeFetchRequest:request error:&error];
NSManagedObject* favoritsGrabbed = [results objectAtIndex:CurrentIndexPath];
[favoritsGrabbed setValue:ratingcoredata forKey:@"rating"];
// Save the object to persistent store
if (![context save:&error]) {
NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
}
}
if(rating<=3)
{
UIAlertView *testAlert = [[UIAlertView alloc] initWithTitle:@"Warning!"
message:@"Please Enter Reason"
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];
}
DataModel *model = [arrData objectAtIndex:rateView.tag];
model.RateViewValue = RateString;
//example reload table
dispatch_async(dispatch_get_main_queue(), ^{
[kpitable reloadData];
});
}
提前致谢!
答案 0 :(得分:0)
您可以使用UITableView Delegate禁用TableView中单个单元格的触摸事件。
https://developer.apple.com/reference/uikit/uitableviewdelegate/1614988-tableview
- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row < 10) {
return YES;
} else {
return NO;
}
}
对于没有突出显示的单元格,不会调用“didSelectRowAtIndexPath”委托方法。
答案 1 :(得分:0)
我会使用当前启用的单元格索引存储NSInteger
属性,例如:
@property (nonatomic, assign) NSInteger currentlyEnabledCellIndex;
将其指定为0。
然后实现此UITableViewDelegate
方法来控制选择:
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.currentlyEnabledCellIndex == indexPath.row) {
return indexPath;
}
else {
return nil;
}
}
选择右侧单元格后,请更新currentlyEnabledCellIndex
:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
self.currentlyEnabledCellIndex = self.currentlyEnabledCellIndex + 1;
}
确保在到达最后一个单元格时将其重新设置为0.