簡體   English   中英

從數據庫獲取URL並作為UITableView的圖像放置

[英]Getting url from Database and putting as an image for UITableView

所有,

我需要將圖片分配給UITableView。 每行都有不同的圖片。 每行的數據均來自數據庫,該數據庫上的行具有ID,另一個數據庫中的ID已鏈接,並且該表中具有圖像的URL。

因此,在cellforRowAtIndexPath中,我們有:NSDictionary * postValues = @ {@“ myParam”:apt [@“ barID”]};

    // Do any additional setup after loading the view, typically from a nib.
    self.client = [MSClient clientWithApplicationURLString:@"https://outnight-mobile.azure-mobile.net/" applicationKey:@"okYeRGfBagYrsbkaqWIRObeDtktjkF10"];
[self.client invokeAPI:@"photos"
                  body:postValues
            HTTPMethod:@"POST"
            parameters:nil
               headers:nil
            completion:^(id result, NSHTTPURLResponse *response, NSError *error) {
                if (error) {
                    NSLog(@"Error %@", error );
                } else
                {
                    NSString *string = [NSString stringWithFormat:@"%@", [result objectForKey:@"rows"]];
                    NSString *stringWithoutbracketsend = [string stringByReplacingOccurrencesOfString:@")" withString:@""];
                    NSString *stringWithoutbracketsfront = [stringWithoutbracketsend stringByReplacingOccurrencesOfString:@"(" withString:@""];
                    NSString *completion = [stringWithoutbracketsfront stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
                    NSString *newStr = [completion substringFromIndex:1];
                    NSString *finalstring = [newStr substringToIndex:newStr.length-(newStr.length>0)];
                    NSLog(@"%@", finalstring);
                    [cell.imageView setImageWithURL:[NSURL URLWithString:finalstring] placeholderImage:[UIImage imageNamed:@"greybox40.png"]];

                }
            }];

這從表中傳入了JSON網址,我刪除了所有標簽,得到的字符串只是http://blag.com/bah.jpg '這很容易。 我正在使用SBWebImage將圖像放入uitableview異步中,這也很好。 將圖片放在網址的每一行上的最佳方法是什么? 用SWITCH語句最好嗎? 最好使用網址數組,然后顯示它們。 最好從cellforIndexRowPath中移出數據庫連接性嗎? 有人可以請教嗎?

首先,為uitableview創建一個自定義視圖單元。 然后將子視圖(例如圖像,標簽)添加到自定義單元格。從數據庫中獲取數據到圖像數組,ID數組在cellforrowatindex的tableview委托方法中使用該代碼。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {靜態NSString * CellIdentifier = @“ Cell”; if(tableView == myTableView){FirstViewCustomCell * cell =(FirstViewCustomCell *)[myTableView dequeueReusableCellWithIdentifier:CellIdentifier]; 如果(cell == nil){[[[NSBundle mainBundle] loadNibNamed:@“ FirstViewCustomCell” owner:self options:nil]; cell = customCell; customCell = nil; }
[cell setSelectionStyle:UITableViewCellSelectionStyleNone]; //從數據庫中獲取數據。 cell.customimage.image = [imagesarr objectatIndex:indexpath.row]; cell.textlabel.text = [arrayofID objectatIndex:indexpath.row]; }

在一種方法中分別解析數據,然后

將圖像存儲在數組中。根據需要,您可以選擇前三個圖像。

如果要將圖像的URL保存在對象中,然后保存在數組中。請使用以下代碼

    for(int i=0;i<3;i++)
    {
    ImagesObject *Obj=[imagesArray objectAtIndex:i];

            [cell.userProfilePicImage setImageWithURL:[NSURL URLWithString:Obj.imageURLString]
                                     placeholderImage:[UIImage imageNamed:@"greybox40.png"]
                                            completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
             {
                 if (!error && image)
                 {

                 }
             }];
        }

        (or) if you are not saving in an object then use below code.

for(int i=0;i<3;i++)
    {

        NSString *imageURLString=[imagesArray objectAtIndex:i];

                [cell.userProfilePicImage setImageWithURL:[NSURL URLWithString:imageURLString]
                                         placeholderImage:[UIImage imageNamed:@"greybox40.png"]
                                                completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
                 {
                     if (!error && image)
                     {

                     }
                 }];
}

希望對您有幫助...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM