【问题标题】:TableView Unable to get DATA which are scrolled up or downTableView 无法获取向上或向下滚动的 DATA
【发布时间】:2016-05-06 11:00:55
【问题描述】:

每当我尝试从单元格中获取数据时,它都会返回空值,但是我发现我无法从 TableView 向上/向下滚动的单元格中读取数据

这是清除我的情况的图像

这是我用来保存数据的代码:

  NSMutableArray *arrTmp = [[NSMutableArray alloc] init];
    for(int i = 0; i<[tblView numberOfRowsInSection:0]; i++){
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        FUCellView  *cell         = [tblView cellForRowAtIndexPath:indexPath];
        UITextField *tfNo         = (UITextField *)[cell viewWithTag:i + 100];
        UIButton    *btnTime      = (UIButton    *)[cell viewWithTag:i + 200];
        UITextField *tfVisit      = (UITextField *)[cell viewWithTag:i + 300];
        UIButton    *btnProvider  = (UIButton    *)[cell viewWithTag:i + 400];

        NSMutableDictionary *theDictionary = [[NSMutableDictionary alloc] init];
        if([tfNo.text length] > 0 && tfNo.text != nil){
            NSString* str = [tfNo.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
            [theDictionary setObject:str forKey:@"number"];
        }
        else{
            [theDictionary setObject:@"" forKey:@"number"];
        }
        if([btnTime.titleLabel.text length] > 0 && btnTime.titleLabel.text != nil){
            NSString* str = [btnTime.titleLabel.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
            [theDictionary setObject:str forKey:@"time"];
        }
        else{
            [theDictionary setObject:@"" forKey:@"time"];
        }
        if([tfVisit.text length] > 0 && tfVisit.text != nil){
            NSString* str = [tfVisit.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
            [theDictionary setObject:str forKey:@"visit_type"];
        }
        else{
            [theDictionary setObject:@"" forKey:@"visit_type"];
        }
        if([btnProvider.titleLabel.text length] > 0 && btnProvider.titleLabel.text != nil){
            NSString* str = [btnProvider.titleLabel.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
            [theDictionary setObject:str forKey:@"provider"];
        }
        else{
            [theDictionary setObject:@"" forKey:@"provider"];
            NSLog(@"ProviderELse");
        }
         [arrTmp addObject:theDictionary];

【问题讨论】:

    标签: objective-c uitableview xcode7


    【解决方案1】:

    UITableview 正在重用单元格。因此,您无法在 for 循环中获取值。这将返回 nil。

    更好的方法是使用数据模型来存储值。

    例如:创建数据模型。给它添加一些属性。并具有这些特性的称重传感器。现在只需要更新模型对象属性。

    将这些模型对象存储在一个数组中。

    终于可以随时准备好数据了。

    【讨论】:

    • 我不熟悉Core Data,将尝试使用两个或更多数组来实现您的方法,谢谢您的回复
    • 谢谢@Abdul Yasin
    【解决方案2】:

    据我了解,您应该将您的数据保存在其他地方(您可能有一个带有数据的模型吗?)然后适合模型集(在cellForRowAtIndexPathUITableView。无需直接从UITableView 设置或获取数据。请阅读官方文档here

    Here 是关于表格视图的很好的一课。祝你好运。

    【讨论】:

    • 用于保存数据 im 使用 Arrays 被擦除 可能经常这样,所以我发现这种方法对我来说更容易,但有一个缺点。好的会考虑你的建议
    • 请阅读 MVC(模型 - 视图 - 控制器)模式,您的视图不应保留任何数据,仅显示它,并且视图不能直接通过控制器与模型交互。如果您的数组经常被擦除尝试将此数组的原始版本保留在某种单例(设计模式)中,并在控制器中使用此数组的副本。
    猜你喜欢
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-09
    • 2015-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多