给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;
}
应该是这样,如果你用addImage的话,在retina显示下它会自动使用e"AddImage@2x"。可能是这样引起的问题。
tableview滚动的话相当于‘再开始’。好像你用了一个布尔型将加载button的初始cell刷新掉了。可以使用一个标题将button一直保持在顶端部分。你可以验证一下cell被重用的时候button是不是会移动。
另外,在tableview cell中button不太好用,因为他们控制触摸的方法大相庭径。这样搭配使用需要进行许多修改才能实现说得过去的效果。以后你用着用着就会遇到了。
是因为cell重用吧。你需要在eles句中加一些代码来删除存在的button。其中一种方法:给button一个tag:
addImage.tag = 10;
然后在else 子句中:
}else{
if (cell viewWithTag:10) [[cell viewWithTag: 10] removeFromSuperview];
...