TEL:400-8793-956
当前位置:程序、服务器

Xcode:从解析中获取字符串并分配给标签(Swift)

提问者: 近期获赞: 浏览人数: 发布时间:2021-02-09 17:25:37

 问:我正在使用发送的文本字段文本和要解析的图像,并希望将文本分配给标题视图中的标签,并将图像分配给collectionview。到目前为止,这是我的代码:

 
类标题:UICollectionReusableView {
 
@IBOutlet弱var groupName:UILabel!
}
 
class ImagesCell:UICollectionViewCell {
 
@IBOutlet弱var imageNew:UIImageView!
}
 
让redirectIdentifier1 =“单元格”
 
类TheNewCollectionViewController:UICollectionViewController {
 
@IBOutlet var imagesCollection:UICollectionView!
var allImages:NSMutableArray = []
 var imagesFile:NSArray = NSArray()
 var组:NSArray = NSArray()
覆盖func viewDidLoad(){
     super.viewDidLoad()
     queryParse()
 }
 //标记:UICollectionViewDataSource
覆盖func numberOfSectionsInCollectionView(collectionView:UICollectionView )-> Int {
     return 1
 }
覆盖func collectionView(collectionView:UICollectionView,numberOfItemsInSection部分:Int)-> Int {
     return imagesFile.count
 }
覆盖func collectionView(collectionView:UICollectionView,cellForItemAtIndexPath indexPath:NSIndexPath)-> UICollectionViewCell {
 
    让单元格= collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier1,forIndexPath:indexPath)为!ImagesCell
    让imageObject = imagesFile.objectAtIndex(indexPath.row)为!PFObject
    让filedImage = imageObject.objectForKey(“ imageFile”)为!PFFile
     filedImage.getDataInBackgroundWithBlock {(data,error)->
         if(error!= nil){
             cell.imageNew.image = UIImage(data:data!)
             self.imagesCollection.reloadData()
         }
     }中的
void    返回
函数}
 func queryParse( ){
     let query = PFQuery(className:“ images”)
     query.findObjectsInBackgroundWithBlock {(对象,错误)->无效
 
        if(error!= nil && objects?.count!= 0){
             self.imagesFile = NSArray(array:objects!)
             self.groups = NSArray(array:objects!)
             self.imagesCollection.reloadData()
         } else {
             print( “ Error:\(error)”)
             self.imagesCollection.reloadData()
         }
     }
 }
覆盖func collectionView(collectionView:UICollectionView,viewForSupplementaryElementOfKind种类:字符串,atIndexPath indexPath:NSIndexPath)-> UICollectionReusableView {
 
    让headerView = collectionView.dequeueReusableSupplementaryViewOfKind(UICollectionElementKindSectionHeader,withReuseIdentifier:“ header”,forIndexPath:indexPath)为!标头
    让objectQuery = PFQuery(className:“ images”)
     objectQuery.findObjectsInBackgroundWithBlock {(对象,错误)->        如果错误,则无效
!= nil {
             headerView.groupName.text = objects.objectForKey(“ groupName”)as!字符串
        } else {
             print(“错误:\(错误)”)
         }
     }
    返回headerView
 }
 
 
答:当您运行查询时,它是否从解析中返回任何内容?我看不到将文本字段中的任何文本保存到Parse的行,因此我不确定何时使用getDataInBackground来获取数据。
 
您还可能必须在if语句中删除该&&运算符,因为如果查询中存在错误,它也将导致数组计数为0。
 
我引用的所有内容都在queyParse()方法的范围内。
 
 
答:还有另一个视图控制器,我可以在其中保存文本和图像。这里是:
 
(IBAction)shareBtn:(id)sender {
 
如果([self.groupField.text isEqual:@“”]){
 
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@“警告!”
                                                消息:@“您需要设置组名!”
                                               委托:自我
                                      cancelButtonTitle:@“确定”
                                       otherButtonTitles:无];
[警报显示];
}其他{
 
NSData *数据= UIImageJPEGRepresentation(self.imageView.image,0.8);
NSString *文件名= [NSString stringWithFormat:@“%@。png”,self.groupField.text];
PFObject * share = [PFObject objectWithClassName:@“ images”];
PFFile * imageFile = [PFFile fileWithName:文件名数据:数据];
[分享setObject:imageFile forKey:@“ imageFile”];
MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view动画:是];
hud.mode = MBProgressHUDModeIndeterminate;
hud.labelText = @“正在上传”;
[hud show:YES];
[imageFile saveInBackgroundWithBlock:^(BOOL成功,NSError * error){
     if(!error){
         //现在图像已上传到Parse。将其与新对象关联
 
 
        PFObject * share = [PFObject objectWithClassName:@“ images”];
        [分享setObject:imageFile forKey:@“ imageFile”];
        [共享setObject:self.groupField.text forKey:@“ groupName”];
        [共享saveInBackgroundWithBlock:^(BOOL成功,NSError *错误){
            如果(!错误){
                 NSLog(@“ Saved”);
                [hud hide:YES];
                [自我performSegueWithIdentifier:@“ returnHome”发送者:无];
            } else {
                 //错误
                NSLog(@“错误:%@%@”,错误,[错误userInfo]);
                UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@“连接错误!”
 
 
                                                                消息:@“再次尝试请求!”
                                                               委托:自我
                                                      cancelButtonTitle:@“确定”
                                                       otherButtonTitles:无];
                [警报显示];
            }
         }];
    }
 }];
上一篇: 无法链接HTML和CSS
下一篇: 创建一个名为max()的新函数,该函数接受两个数字作为参数。该函数应返回较大的t