简体   繁体   English

在UITableViewCell中动态显示图像

[英]Dynamically display an image in a UITableViewCell

I have a UITableViewCell, it is scroll-disabled and with fixed sections and rows. 我有一个UITableViewCell,它被禁用滚动并且具有固定的部分和行。

There are two sections, with 1 row in section 0, and several rows in section 1. 有两部分,第0部分有1行,第1部分有几行。

The tableView is for users to make some choices. tableView供用户做出一些选择。

So, the first section (with only one row) is going to display the result of users' choices, 因此,第一部分(只有一行)将显示用户选择的结果,

and no doubt the second section (with several rows) is for choosing. 毫无疑问,第二部分(有几行)可供选择。

Now I want to put an image in the cell of the only row of the first section, 现在,我想在第一部分的第一行的单元格中放置一个图像,

and this image will change according to users' choose. 并且此图像将根据用户的选择而改变。

It is very easy to judge which png image should be displaying, but I have trouble update it. 判断应该显示哪个png图像非常容易,但是我无法对其进行更新。

I tried use the cell's imageView, and manually alloc a UIImageView or UIView there to display those images. 我尝试使用单元格的imageView,并在那里手动分配一个UIImageView或UIView来显示这些图像。

But all of them won't work, I mean they just keep what they are displaying at the beginning and never changes it, even if I set the view's background or its image to a new png. 但是所有这些都不起作用,我的意思是,即使我将视图的背景或图像设置为新的png,它们也只会保留它们在开始时显示的内容,并且永远不会更改。

I tried some method like 我尝试了一些方法

[myImage setNeedsDisplay] for the manually alloced view, [myImage setNeedsDisplay]用于手动分配的视图,

or 要么

[thatCell setNeedsDiaplsy] & [self.tableView reloadData] for the imageView of that cell, 该单元格的imageView的[thatCell setNeedsDiaplsy]和[self.tableView reloadData],

but in vain. 但徒劳无功。

So I wonder how can I achieve this function that dynamically display an image in a UITableViewCell in different situations? 因此,我想知道如何实现在不同情况下在UITableViewCell中动态显示图像的功能?

Thanks a lot! 非常感谢!

__ _ __ update line _ ____ __ _ __ 更新行 _ ____

I'm sorry that I didn't provide my code. 对不起,我没有提供我的代码。

and here they are. 他们在这里。

- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     static NSString *inOutTableCellIdentifier = @"DealTableViewIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:inOutTableCellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
                                   reuseIdentifier:inOutTableCellIdentifier] autorelease];
    cell.textLabel.font = [UIFont fontWithName:cMyFont size:[UIFont systemFontSize]];
    cell.detailTextLabel.font = [UIFont fontWithName:cMyFont size:[UIFont smallSystemFontSize]];
     // I tried using both imageView and UIView here, but won't work

     if (indexPath.section == 0) {   
        //cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"moneyCell.png"]];
        cell.backgroundColor = [UIColor cyanColor];
       // cell.imageView.image = [UIImage imageNamed:@"dealDone2.png"];
        self.undoneIcon = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)] autorelease];
        //self.undoneIcon.image = [UIImage imageNamed:@"dealUndone2.png"];
        self.undoneIcon.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"dealUndone2.png"]];
        [cell.contentView addSubview:self.undoneIcon];
        ....  // deal with other rows in section 1
        return cell;
} 
// and this is the method that update the image, the images are named "dealDoneX.png",
// where X is an integer from 0 to 4.
- (void)checkUndoneDegree {  // here I also tried 2 ways corresponding to UIView or a cell's imageView, but neither works well.
int i = 0;
if (self._date)
    i++;
if (self.moneyTextField.text)
    i++;

if (self._incomingAccount)
    i++;
if (self._expensingAccount)
    i++;
if (_dealType != kTransferView)
    if (self._type)
        i++;
NSLog(@"undone degree: %d",i);
NSString *imageName = [@"dealUndone" stringByAppendingFormat:@"%d",i];
self.undoneIcon.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:imageName]];
[self.undoneIcon setNeedsDisplay];
//    NSUInteger p[2] = {0,0};
//    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath    indexPathWithIndexes:p length:2]];
//    [cell setNeedsDisplay];
}

and everytime I update the table's data, like changing some text of some cell, 每当我更新表格的数据时,例如更改某些单元格的文本,

I would call [self checkUndoneDegree] and then call [self.tableView reloadData], 我会调用[self checkUndoneDegree],然后调用[self.tableView reloadData],

But the picture is never updated, at least from the screen. 但是图片永远不会更新,至少从屏幕上是不会更新的。

I even tried to put the codes that decide which png to set in the 我什至尝试将决定设置哪个png的代码放入

  • (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

method, but it can only make the view displaying the first png, without any updating. 方法,但是它只能使视图显示第一个png,而不进行任何更新。

Thanks for helping! 感谢您的帮助!

  1. Make your undoneDegree (represented by i variable in your code) an ivar of your view controller, so it is accessible in all of it's methods, also in the UITableView delegate and data source methods. undoneDegree (由代码中的i变量表示)设置为视图控制器的ivar,以便可以在其所有方法中访问它,也可以在UITableView委托和数据源方法中访问它。
  2. Forget about setNeedsDisplay method. 忘记setNeedsDisplay方法。 Since you are using UITableView to display your content, you need to play by its rules. 由于您正在使用UITableView来显示您的内容,因此需要按照其规则进行播放。 This means you should use reloadSections:withRowAnimation: method. 这意味着您应该使用reloadSections:withRowAnimation:方法。
  3. Check again if you need self.undoneIcon . 再次检查是否需要self.undoneIcon I'm pretty sure that imageView property should do. 我很确定imageView属性应该可以。 That is if you really want to display an image in the cell. 也就是说,如果您确实要在单元格中显示图像。 If you want to create some kind of progress bar by manipulating cell's background, then use backgroundView property, or just place UIProgressView in your cell. 如果要通过操纵单元格的背景来创建某种进度条,请使用backgroundView属性,或将UIProgressView放置在单元格中。 This is what I think your want to do, after looking at your code. 查看代码后,这就是我想您要执行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM