【问题标题】:How to Enable and Disable Table View cell one by one in objective c如何在目标c中一一启用和禁用表格视图单元格
【发布时间】:2017-01-13 01:28:18
【问题描述】:

我是 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];
    });

}

提前致谢!

【问题讨论】:

  • 只是为了确保我理解,您只希望启用第一个单元格。然后当它被点击时,你只想启用第二个单元格,等等?
  • @mattsson 是的,我想要这样。
  • @Muju 你还在为同一个项目苦苦挣扎吗?
  • @Gulliva 是的。这是同一个项目,但不同的问题。

标签: ios objective-c uitableview


【解决方案1】:

您可以使用 UITableView 委托来禁用 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”委托方法。

【讨论】:

    【解决方案2】:

    我将使用当前启用的单元格索引存储一个 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。

    【讨论】:

    • 否 它选择表格视图单元格中的所有值。
    • 我不明白你的意思。
    • @mattson 我认为按钮点击不在 didselectrowatindexpath 中。
    • 什么意思?您采用哪个值?
    • 我正在使用自定义表格视图并使用费率视图。我在费率视图委托上获得价值 - (void)rateView:(RateView *)rateView ratingDidChange:(int)rating
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-28
    • 2011-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多