2014-02-20 44 views
0

我試圖在UICollectionViewCell中獲取簡單UITextView的文本內容。基本上,當用戶共享某個單元格時,我想抓取該單元格中寫入的內容以便在ActivityViewController中使用。UITextView說它不是空時

事情是,它說它是空白的。這顯然不是空白的:當我點擊分享時,我正在看它,它裏面有文字。 (該文本是拍攝照片的位置的反向查找地址。)

我非常缺乏經驗的眼睛認爲問題在於TextView正在被塊填充。我不知道爲什麼這會是一個問題,但它似乎跳出來對我。

下面是是應該抓住它用於設置單元格(並因此設置TextView的文本)的方法,其次是分享方法:

-(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; 
} 

這一切似乎很好地工作。但隨後在下面的分享方法,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]; 
} 

我是正確的思維該塊是什麼引起的字符串locationName是在分享方法是空的? (它已正確初始化,只是一個空字符串)。如果是這樣,是否有解決方法?或者,如果沒有,任何想法爲什麼locationName出現空白,即使textViewDescription有文字在裏面?

所有我想要的是讓該字段中的文字...

+2

如果我記得很清楚,你CoreLocation塊是異步的,當你'sharePic',是否已完成?您是否檢查* currentCell *是否不是*零*? – Larme

+1

不要從單元格中取回文本。文本應該在你的數據模型中,你應該從那裏得到它... – Wain

+0

@Larme currentCell不是零,因爲我可以從其中拉出其他東西,比如早先設置的'labelMain',沒有任何問題。 – Nerrolken

回答

1

不要讓文字從小區後面。該文本應該在你的數據模型,你應該從那裏得到它...

這就是說,你的問題是無關塊(儘管異步操作可能會導致你的問題有時)。問題在於你正在創建一個新的單元而不是獲取現有單元。相反的:

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

你應該有:

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

啊哈!謝謝!是的,我正在尋找更好的解決方案,但感謝您的答案! – Nerrolken

相關問題