Iphone uitableview上的按钮
uitableview上的按钮有问题。在代码中,当我单击每个单元格的按钮时,按钮上应该显示image Checked.png。这很好用。但是,当我滚动tableview时,表格顶部的选中图像消失了。我该如何解决这个问题。有什么好心人能帮我吗Iphone uitableview上的按钮,iphone,objective-c,Iphone,Objective C,uitableview上的按钮有问题。在代码中,当我单击每个单元格的按钮时,按钮上应该显示image Checked.png。这很好用。但是,当我滚动tableview时,表格顶部的选中图像消失了。我该如何解决这个问题。有什么好心人能帮我吗 //table.m// - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //- (UITabl
//table.m//
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//- (UITableViewCell *)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
[[NSBundle mainBundle] loadNibNamed:@"TableCell" owner:self options:NULL];
cell = nibLoadedCell;
button *toggleControl = [[CheckBox alloc] initWithFrame: CGRectMake(270.0, 10.0, 60.0, 60.0)];
toggleControl.tag = toggleControlTag;
[cell.contentView addSubview: toggleControl];
[toggleControl addTarget:self action:@selector(Selected:) forControlEvents:UIControlEventTouchDown];
}
//button.m//
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[self setImage:[UIImage imageNamed:@"Uncheck.png"] forState:UIControlStateNormal];
[self addTarget:self action:@selector(checkBoxClicked) forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
-(IBAction) checkBoxClicked{
if(self.isChecked ==NO){
self.isChecked =YES;
[self setImage:[UIImage imageNamed:@"Checked.png"] forState:UIControlStateNormal];
}else{
self.isChecked =NO;
[self setImage:[UIImage imageNamed:@"Uncheck.png"] forState:UIControlStateNormal];
}
}
您正在按钮中存储按钮的状态。这是错误的做法。按钮只是状态的视图。您需要将数据存储在其他位置,例如数组中,并根据该单元格的indexPath从数组中提取表单元格的相关数据 当有人按下您的按钮时,您可以(例如)在数据模型中设置布尔值,以指示此特定选项是由用户选择的 您的单元格将始终滚动离开视图,这将导致单元格被重新使用,并且您的按钮将不再“有效”。因此,对于创建的每个单元格,您需要检查模型中该数据特定部分的数据,并相应地填充视图(=单元格)
这不会损害请将所有代码标记为代码(突出显示并单击101010按钮)。这样读是不可能的。