我有一个 UITableViewCell,它是滚动禁用的,并且具有固定的部分和行。
有两个部分,第 0 部分有 1 行,第 1 部分有几行。
tableView是供用户做出一些选择的。
因此,第一部分(只有一行)将显示用户选择的结果,
毫无疑问,第二部分(有几行)是供选择的。
现在我想将图像放入第一部分唯一行的单元格中,
并且该图像会根据用户的选择而变化。
很容易判断应该显示哪个png图像,但我在更新它时遇到了麻烦。
我尝试使用单元格的 imageView,并手动分配 UIImageView 或 UIView 来显示这些图像。
但是它们都不起作用,我的意思是它们只是保留它们在开始时显示的内容并且永远不会更改它,即使我将 View 的背景或其图像设置为新的 png。
我尝试了一些方法,例如
[myImage setNeedsDisplay] 用于手动分配的 View ,
或
[thatCell setNeedsDiaplsy] & [self.tableView reloadData] 该单元格的 imageView,
但徒劳。
所以我想知道如何才能实现在不同情况下动态显示 UITableViewCell 中的图像的功能?
非常感谢!
_____更新行_ ___
很抱歉,我没有提供我的代码。
他们就在这里。
- (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];
}
每次我更新表格的数据时,例如更改某些单元格的某些文本,
我会调用[self checkUndoneDegree],然后调用[self.tableView reloadData],
但是图片永远不会更新,至少从屏幕上看是这样。
我什至尝试将决定要设置哪个png的代码放在
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
方法,但它只能使 View 显示第一个png,而不进行任何更新。
感谢您的帮助!
最佳答案
- 使您的 undoneDegree (由代码中的 i 变量表示)成为 View Controller 的 ivar,以便可以在其所有方法中访问它,也可以在 UITableView 中访问委托(delegate)和数据源方法。
- 忘记setNeedsDisplay方法。由于您使用 UITableView 来显示内容,因此您需要遵守其规则。这意味着您应该使用 reloadSections:withRowAnimation:方法。
- 再次检查您是否需要self.undoneIcon。我很确定imageView属性(property)应该做。也就是说,如果您确实想在单元格中显示图像。如果您想通过操作单元格的背景来创建某种进度条,请使用 backgroundView属性(property),或只是地方UIProgressView在你的牢房里。在查看您的代码后,我认为这就是您想要做的。
关于ios - 在 UITableViewCell 中动态显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9280140/