“带有自定义单元格的表格视图”的问题
我有一个带有自定义单元格和 5 个部分的表格视图。我使用自定义单元格“如果我点击一行,图像应该改变”。它工作正常。但是,在选择第一部分中的一行后,它还会选择最后一部分中的任意一行。如果我上下滚动,第一个和最后一个部分的行的单元格图像将以随机方式移动。
请帮助...!!!
我是 iphone 开发的初学者。
以下是我的表视图中的 cellforrow 代码。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *kCustomCellID = @"MyCellID";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:kCustomCellID];
if (cell == nil)
{
cell = (CustomCell *) [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID]autorelease];
}
NSArray *localperson = [personArray objectAtIndex:indexPath.section];
cell.textLabel.text = [localperson objectAtIndex:indexPath.row];
return cell; }
以下是 CustomCell 的代码
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier])
{
self.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
// cell's check button
checkButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
checkButton.frame = CGRectZero;
checkButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
checkButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[checkButton addTarget:self action:@selector(checkAction:) forControlEvents:UIControlEventTouchDown];
checkButton.backgroundColor = self.backgroundColor;
[self.contentView addSubview:checkButton];
}
return self; }
- (void)layoutSubviews { [super layoutSubviews];
CGRect contentRect = [self.contentView bounds];
CGRect frame = CGRectMake(contentRect.origin.x + 40.0, 8.0, contentRect.size.width, 30.0);
self.textLabel.frame = frame;
// layout the check button image
UIImage *checkedImage = [UIImage imageNamed:@"checked.png"];
frame = CGRectMake(contentRect.origin.x + 10.0, 12.0, checkedImage.size.width, checkedImage.size.height);
checkButton.frame = frame;
UIImage *image = (self.checked) ? checkedImage: [UIImage imageNamed:@"unchecked.png"];
UIImage *newImage = [image stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
[checkButton setBackgroundImage:newImage forState:UIControlStateNormal]; }
提前致谢。
I have a tableview with custom cells and with 5 sections. I used custom cell for "if I tap a row, the image should change". it is working fine. but, after selecting a row in first section, it also selects any one of the row in the last section. and if I scroll up and down, the first and last section's row's cell images are shifting in a random manner.
plz help...!!!
I'm a beginner in iphone development.
The following is the cellforrow code in my table view.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *kCustomCellID = @"MyCellID";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:kCustomCellID];
if (cell == nil)
{
cell = (CustomCell *) [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID]autorelease];
}
NSArray *localperson = [personArray objectAtIndex:indexPath.section];
cell.textLabel.text = [localperson objectAtIndex:indexPath.row];
return cell; }
The following is the code for CustomCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier])
{
self.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
// cell's check button
checkButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
checkButton.frame = CGRectZero;
checkButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
checkButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
[checkButton addTarget:self action:@selector(checkAction:) forControlEvents:UIControlEventTouchDown];
checkButton.backgroundColor = self.backgroundColor;
[self.contentView addSubview:checkButton];
}
return self; }
- (void)layoutSubviews { [super layoutSubviews];
CGRect contentRect = [self.contentView bounds];
CGRect frame = CGRectMake(contentRect.origin.x + 40.0, 8.0, contentRect.size.width, 30.0);
self.textLabel.frame = frame;
// layout the check button image
UIImage *checkedImage = [UIImage imageNamed:@"checked.png"];
frame = CGRectMake(contentRect.origin.x + 10.0, 12.0, checkedImage.size.width, checkedImage.size.height);
checkButton.frame = frame;
UIImage *image = (self.checked) ? checkedImage: [UIImage imageNamed:@"unchecked.png"];
UIImage *newImage = [image stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
[checkButton setBackgroundImage:newImage forState:UIControlStateNormal]; }
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 cellForRowAtIndexPath 上,您需要将 self.checked 设置为适当的值。由于表视图重复使用滚出屏幕的单元格,因此当您滚动时,您会看到这些重复使用的单元格重新出现在新位置。您设置的内容正确,但您还需要设置选中的值。
On your cellForRowAtIndexPath you need to set
self.checked
to the appropriate value. Since the table view reuses cells that roll off the screen, you are seeing those reused cells reappear in new positions as you scroll. You are setting the content correctly but you need to set the checked value as well.您的细胞正在被重复利用。
重用的单元格具有
checked
属性,该属性可能仍具有单元格先前使用时的值。确保在
cellForRowAtIndexPath
中将选中的值设置为“no”Your cell is getting reused.
The reused one has a
checked
property that may still have the value from the cell's previous usage.Make sure that you set the checked value to 'no' in
cellForRowAtIndexPath