lianlianbushell 2013-04-25 02:22 采纳率: 0%
浏览 2905
已采纳

UITableView Cell Button问题

给UITableView设置了一个button。

在viewDidLoad执行完,button就在右边,但是如果进行滚动。button就开始乱跑。

代码如下:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}

if (indexPath.section == 0 && indexPath.row == 0 && _isAddImageViewLoad == NO) {
    // Add Image Button
    UIButton *addImage = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImage* image = [UIImage imageNamed:@"AddImage@2x"];
    addImage.frame = CGRectMake(110.0f, 10.0f, 110.0f, 110.0f);
    [addImage setImage:image forState:UIControlStateNormal];

    [cell.contentView addSubview:addImage];
    _isAddImageViewLoad = YES;
} else {
    NSDictionary *dictionary = [_items objectAtIndex:indexPath.section];
    NSArray *array = [dictionary objectForKey:@"data"];
    NSString *cellValue = [array objectAtIndex:indexPath.row];
    cell.textLabel.text = cellValue;
}
return cell;
}
  • 写回答

2条回答 默认 最新

  • Mr_me 2013-04-25 03:30
    关注

    应该是这样,如果你用addImage的话,在retina显示下它会自动使用e"AddImage@2x"。可能是这样引起的问题。

    tableview滚动的话相当于‘再开始’。好像你用了一个布尔型将加载button的初始cell刷新掉了。可以使用一个标题将button一直保持在顶端部分。你可以验证一下cell被重用的时候button是不是会移动。

    另外,在tableview cell中button不太好用,因为他们控制触摸的方法大相庭径。这样搭配使用需要进行许多修改才能实现说得过去的效果。以后你用着用着就会遇到了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64