ios - UITextView 表示它在不存在时为空

标签 ios uitextview objective-c-blocks

我正在尝试获取 UICollectionViewCell 内简单 UITextView 的文本内容。基本上,当用户共享某个单元格时,我想获取该单元格中写入的内容以在 ActivityViewController 中使用。

问题是,它说它是空白的。它显然不是空白的:当我单击“共享”时,我正看着它,里面有文本。 (文字为拍照地点的反向查找地址。)

我非常缺乏经验的眼睛认为问题在于 TextView 正在由 block 填充。我不知道为什么这会成为一个问题,但它似乎让我感到惊讶。

下面是设置单元格的方法(从而设置 TextView 的文本),然后是应该抓取它的 Share 方法:

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"photoCell" forIndexPath:indexPath];

    ALAsset *asset = [_photosArray objectAtIndex:indexPath.row];
    ALAssetRepresentation *assetRep = [asset defaultRepresentation];

    [cell.labelMain setText:[assetRep filename]];

    CLLocation *location = [asset valueForProperty:ALAssetPropertyLocation];
    CLGeocoder *geocoder = [[CLGeocoder alloc] init] ;
    [geocoder reverseGeocodeLocation:location
                   completionHandler:^(NSArray *placemarks, NSError *error) {
                       if (error){
                           NSLog(@"Geocode failed with error: %@", error);
                           return;
                       }
                       CLPlacemark *placemark = [placemarks objectAtIndex:0];

                       NSArray *addressArray = [NSArray arrayWithArray:[placemark.addressDictionary valueForKey:@"FormattedAddressLines"]];
                       [cell.textViewDescription setText:[NSString stringWithFormat:@"%@\n%@\n%@", [addressArray objectAtIndex:0], [addressArray objectAtIndex:1], [addressArray objectAtIndex:2]]];
                   }];

    return cell;
}

这一切似乎都很完美。但在下面的 Share 方法中,locationName 莫名其妙地变成了空白:

-(void)sharePic {
    PhotoCell *currentCell = (PhotoCell*)[self collectionView:_collectionViewMain cellForItemAtIndexPath:[NSIndexPath indexPathForItem:_selectedIndex inSection:0]];

    NSString *locationName = [[currentCell textViewDescription] text];
    NSArray *arrayOfActivityItems = [NSArray arrayWithObjects:locationName, nil];

    UIActivityViewController *activityVC = [[UIActivityViewController alloc]
                                            initWithActivityItems:arrayOfActivityItems applicationActivities:nil];

    [self.navigationController presentViewController:activityVC animated:YES completion:nil];
}

我是否正确地认为该 block 是导致 Share 方法中字符串 locationName 为空的原因? (它已正确初始化,只是一个空字符串)。如果是这样,有办法解决吗?或者如果没有,知道为什么即使 textViewDescription 中有文本,locationName 显示为空白吗?

我想要的只是获取该字段中的文本...

最佳答案

不要从单元格中取回文本。文本应该在您的数据模型中,您应该从那里获取它...

也就是说,您的问题与 block 无关(尽管异步操作有时可能会导致您出现问题)。问题是您正在创建一个新单元格而不是获取现有单元格。而不是:

PhotoCell *currentCell = (PhotoCell*)[self collectionView:_collectionViewMain cellForItemAtIndexPath:[NSIndexPath indexPathForItem:_selectedIndex inSection:0]];

你应该有:

PhotoCell *currentCell = (PhotoCell*)[self.collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForItem:_selectedIndex inSection:0]];

关于ios - UITextView 表示它在不存在时为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21902301/

相关文章:

ios - 如何将 didFinishEditing (textField) 与自定义单元格一起使用?

ios - 如何设置 Apple 推送通知服务中的声音? (总是默认的?)

iphone - 确保 block 内的方法在调用另一个方法之前完成执行

ios - 将 block 保存在字典中

ios - 维护 Realm 的多个实例

iOS:使用 NSTask 检查 SVN checkout 是否成功

ios - 模仿 UITextView 的默认双击行为

ios - 更改高度时,UITextView 的 textContainer 在错误的帧呈现

ios - 表格 View 重叠单元格swift2

objective-c - 如何使用NSFileHandle的writeabilityHandler?