簡體   English   中英

在UITableViewCell中的UIButton上更新imageView

[英]Updating imageView on UIButton within UITableViewCell

我有自定義的UITableViewCells (在界面生成器中創建),這些UITableView用於靜態UITableView ,出於參數考慮,我將其稱為AddUserTableViewController (即,我不使用自動釋放的單元格,因為我只有5行)。

在頂部的單元格(用於用戶名 )中,我嵌入了一個UIButton ,用於打開UIActionSheet ,該UIActionSheet繼而允許UIImagePicker在UIButton上設置圖像(即[button.imageView.image = imageFromImagePicker] )。 該按鈕有一個默認圖像(在IB中添加),顯示為“添加圖像”。

按鈕上的圖像設置沒有問題,即使我導航到任何表單元格的詳細信息頁面,但包含該按鈕的用戶名單元格除外,該圖像仍保留在原位。 當我選擇此單元格時,圖像消失,並且一旦從“添加名稱”詳細信息視圖導航回到AddUserTableViewController ,就不會再出現。 顯示上面提到的“添加圖像”默認圖像。

我嘗試了許多策略,其中包括:

1)使用[self.tableView reloadData] [button.imageView setNeedsDisplay] [self.view setNeedsDisplay]viewWillAppear方法;

2)在cellForRowAtIndexPath方法中使用以上方法;

3)在重寫的willDisplayCell:forRowAtIndexPath:方法中使用以上方法;

當我在程序中放置調試NSLog語句時,我可以看到button.imageView.image屬性仍設置為使用UIImagePicker選擇的圖像,但未顯示(顯示了筆尖的默認圖像)。

如上所述,僅當我通過選擇嵌入UIButtonUITableViewCell離開AddUserTableViewController時,才會發生這種情況。

我目前對如何做不知所措,非常感謝任何人都可以提供的任何幫助。 我只需要找到一種在上述情況下更新嵌入式UIButton上圖像的方法。

我已經從cellForRowAtIndexPath:方法中添加了一段代碼,僅用於說明目的-

//populates the personal info section
if (section == kPersonalInfoAddSection) {

    //Covers the add image button and the name adding field
    if (row == kNameRow) {


        UILabel *nameLabel = (UILabel *)[nameCell viewWithTag:kMainTextTag];
        UILabel *reqLabel = (UILabel *) [nameCell viewWithTag:kSubTextTag];
        //UIButton *imageButton = (UIButton *) [nameCell viewWithTag:kImageTag];

        if (mainUser.imagePath != nil) {

            UIImage *img = [UIImage imageWithContentsOfFile:mainUserImagePath];
            imageButton.imageView.image = img;
            [imageButton.imageView setNeedsDisplay];
            //[imageButton setNeedsDisplay];
            //[nameCell setNeedsDisplay];
            NSLog(@"************************** Added the BESPOKE image (%@)to the image button", img);
        }
        else {
            NSLog(@"************************** Added the DEFAULT image (%@)to the image button", addUserImage);
            imageButton.imageView.image = addUserImage;
        }


        UIColor *blackText = [[UIColor alloc] initWithWhite:0 alpha:1];


        NSString *firstName;

        if (mainUser.firstName){

            nameLabel.textColor = blackText;
            firstName = mainUser.firstName;

        }
        else {

            nameLabel.textColor = reqLabel.textColor;
            firstName = NAME_STRING;
        }

        NSString *lastName = (mainUser.lastName)? mainUser.lastName : EMPTY_STRING;


        NSString *name = [[NSString alloc] initWithFormat:@"%@ %@", firstName, lastName];

        nameLabel.text = name;
        reqLabel.text = REQUIRED_STRING;

        [blackText release];

        return nameCell;

    }

cellForRowAtIndexPath中,您是否正在進行某些操作以“重置”圖像?

暫無
暫無

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

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