Iphone UITableViewCell在向下和向上滚动UITableView时获取默认图像
当我选中一个单元格时,关联的图像被更改,但当我向下和向上滚动时,单元格得到了它的默认图像这是我的代码:Iphone UITableViewCell在向下和向上滚动UITableView时获取默认图像,iphone,objective-c,uitableview,ios5,Iphone,Objective C,Uitableview,Ios5,当我选中一个单元格时,关联的图像被更改,但当我向下和向上滚动时,单元格得到了它的默认图像这是我的代码: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier"; NSArray
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
NSArray *listData =[self->tableContents objectForKey:
[self->sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
UIImageView *unchecked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"unchecked.png"]];
UIImageView *checked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checked.png"]];
BOOL bChecked = NO;//By default, it's unchecked, so NO
/// Assign cell according to `bChecked`
cell.accessoryView = (bChecked ? checked : unchecked);
return cell;
}
编辑:
我已经将代码编辑为如上所示,但仍然存在问题,当我向上滚动我的表视图时,会返回默认图像
编辑2:
@Openside:根据您的方法,我的代码片段应该如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
NSArray *listData =[self->tableContents objectForKey:
[self->sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
}
NSUInteger row = [indexPath row];
textClass *myText = (textClass*)[listData objectAtIndex:row];
cell.textLabel.text = myText.text;
UIImageView *unchecked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"unchecked.png"]];
UIImageView *checked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checked.png"]];
cell.accessoryView = (myText.isChecked ? checked : unchecked);
return cell;
}
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
textClass *myText=[[textClass alloc]init];
myText.isChecked=YES;
}
我得到了一个例外:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString text]: unrecognized selector sent to instance 0x673bc'
指示异常问题的行是这样的(在cellforrowatinexpath
delegate方法中):
我已经添加了类textClass,虽然我不太确信,但我认为我可以在我的UITableView的holder类中使用属性。问题是,当您将单元格从视图中滚动出来然后返回视图时,它将被刷新(或重新使用,因此是dequeueReusableCellWithIdentifier)。您需要在源对象上设置一个标志来说明是否选中了它,然后添加一个if语句来确定是使用checked.png还是unchecked.png图像 然而,从您的代码来看,源对象只是文本,创建一个具有两个属性的子类NSObject可能是值得的 文本和 检查 然后,您的cellForRowAtIndexPath代码可以确定要显示的图像 我希望这有帮助
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
}
NSUInteger row = [indexPath row];
textClass *myText = (textClass*)[listData objectAtIndex:row];
cell.textLabel.text = myText.text;
UIImageView *unchecked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"unchecked.png"]];
UIImageView *checked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checked.png"]];
/// Assign cell according to `bChecked`
cell.accessoryView = (myText.isChecked ? checked : unchecked);
return cell;
}
@interface textClass : NSObject {
@private
NSString *_text;
BOOL _isChecked;
}
@property (nonatomic, retain) NSString *text;
@property (nonatomic, assign) BOOL isChecked;
@end
@implementation textClass
@synthesize text = _text;
@synthesize isChecked = _isChecked;
@end
好的,我在这里创建了一个名为textClass的类,它有两个属性text,并被选中。加载视图时,使用以前使用的文本用这些对象填充NSMutableArray(listData)。当您的单元格被选中时,当单元格被重用时,将isChecked属性设置为YES。此属性已在委托方法之外保留其状态,并应正确呈现。问题是单元格将被刷新(或重用,因此将重新使用dequeueReusableCellWithIdentifier)当您将其从视图中滚动出来,然后再回到视图中时。您需要在源对象上设置一个标志来说明是否选中了它,然后添加一个if语句来确定是使用checked.png还是unchecked.png图像 然而,从您的代码来看,源对象只是文本,创建一个具有两个属性的子类NSObject可能是值得的 文本和 检查 然后,您的cellForRowAtIndexPath代码可以确定要显示的图像 我希望这有帮助
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
}
NSUInteger row = [indexPath row];
textClass *myText = (textClass*)[listData objectAtIndex:row];
cell.textLabel.text = myText.text;
UIImageView *unchecked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"unchecked.png"]];
UIImageView *checked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checked.png"]];
/// Assign cell according to `bChecked`
cell.accessoryView = (myText.isChecked ? checked : unchecked);
return cell;
}
@interface textClass : NSObject {
@private
NSString *_text;
BOOL _isChecked;
}
@property (nonatomic, retain) NSString *text;
@property (nonatomic, assign) BOOL isChecked;
@end
@implementation textClass
@synthesize text = _text;
@synthesize isChecked = _isChecked;
@end
好的,我在这里创建了一个名为textClass的类,它有两个属性text,并被选中。加载视图时,使用以前使用的文本用这些对象填充NSMutableArray(listData)。当您的单元格被选中时,当单元格被重用时,将isChecked属性设置为YES。此属性已在委托方法之外保留其状态,并应正确呈现。正如Openside所说,滚动时,单元格将从队列中出来。因此,我们需要根据新的状态分配图像
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = ...; ///< reuse a created cell.
if(cell == nil) {
cell = ...; ///< create a new cell
}
BOOL bChecked = ...///< Decide the cell checked or not.
/// Assign cell according to `bChecked`
cell.accessoryView = (bChecked ? imgChecked : imgNonChecked);
return cell;
}
正如Openside所说,滚动时,单元格将从队列中出来。因此,我们需要根据新的状态分配图像
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = ...; ///< reuse a created cell.
if(cell == nil) {
cell = ...; ///< create a new cell
}
BOOL bChecked = ...///< Decide the cell checked or not.
/// Assign cell according to `bChecked`
cell.accessoryView = (bChecked ? imgChecked : imgNonChecked);
return cell;
}
将完整的代码放在括号中:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
NSArray *listData =[self->tableContents objectForKey:
[self->sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
NSUInteger row = [indexPath row];
textClass *myText = (textClass*)[listData objectAtIndex:row];
cell.textLabel.text = myText.text;
UIImageView *unchecked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"unchecked.png"]];
UIImageView *checked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checked.png"]];
cell.accessoryView = (myText.isChecked ? checked : unchecked);
}
return cell;
}
这肯定会起作用。将完整的代码放在括号中:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
NSArray *listData =[self->tableContents objectForKey:
[self->sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
NSUInteger row = [indexPath row];
textClass *myText = (textClass*)[listData objectAtIndex:row];
cell.textLabel.text = myText.text;
UIImageView *unchecked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"unchecked.png"]];
UIImageView *checked = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"checked.png"]];
cell.accessoryView = (myText.isChecked ? checked : unchecked);
}
return cell;
}
这肯定会奏效。您好,谢谢您的回复,我已经修改了您的解决方案,但我仍然有相同的问题,请查看我的编辑,我已经在我的问题帖子中编辑了代码。@Malek我想您有点遗漏了这一点,在您的示例中,bChecked从何处获得它的值?它只设置一次,并且该值为否。您需要将对象的选中状态保留在委托方法之外,以便在重用单元格时,该方法可以设置状态。如果有帮助的话,我可以用一些代码来详细说明。如果你能在一些代码中进一步阐明你的想法,那将是非常有帮助的,因为我试图将布尔值作为一个全局变量,但仍然没有得到我想要的,我尝试了几种方法:(@Malek我在上面的回答中添加了一些代码,希望能让它更清楚。在您的编辑中,bChecked的作用域有限,它只存在于CellForRowatineXpath中,因此不会影响任何内容。@Openside我将检查它并让您知道:)您好,谢谢您的回复,我已经修改了您的解决方案,但我仍然有相同的问题,请查看我的编辑,我在我的问题帖子中编辑了代码。@Malek我想您有点遗漏了这一点,在您的示例中,bChecked从何处获取其值?它只设置了一次,并且该值为否。您需要将选中状态保持为o委托方法之外的对象,以便在重用单元格时,该方法可以设置状态。如果这有帮助,我可以用一些代码进行详细说明。如果您在一些代码中进一步阐明您的想法,这将非常有帮助,因为我试着将布尔值作为全局变量,但仍然没有得到我想要的,我尝试了几个方法:(@Malek我在上面的回答中添加了一些代码,希望能让它更清楚。在您的编辑中,bChecked的作用域有限,它只存在于CellForRowatineXpath中,因此不会影响任何内容。@Openside我将检查并让您知道:)@Malek让我知道它是如何运行的,不要忘记接口和实现需要在一个单独的文件中,listData需要是您的表视图控制器中的一个NSMutableArray,可能最好在loadView中填充它。您好Openside,thanx对于您的方法,实际上,我遇到了一个例外:
由于未捕获exc而终止应用程序“NSInvalidArgumentException”概念,原因:'-[\uU NSCFConstantString text]:无法识别的选择器
,我将更新我的帖子以向您展示我所做的。我认为使用另一个类来管理所有这些的方法是不可避免的,但问题是每次显示UITableViewCell
时都会调用cellforrowatinexpath
方法,因此我似乎无法声明我的n的实例ew类,因为它将