我有一个自定义的UITableView单元格,我添加了一个文本框进行编辑,显示和隐藏基于编辑模式。我也尝试添加一条垂直线条,用于显示编辑时的情况,而且这样做,但我遇到了一些绘图问题。我刚刚添加了一个绿色复选标记rightView,开始处理输入验证反馈,并且我看到类似的问题。自定义UITableViewCell重绘问题
这是单元格的代码,也是我的cellForRowAtIndexPath的一部分。
#import <UIKit/UIKit.h>
@interface EditableCellStyle2 : UITableViewCell {
CGRect editRect;
UITextField *editField;
UIView *lineView;
}
@property (nonatomic, readonly, retain) UITextField *editField;
@property (nonatomic, readonly, retain) UIView *lineView;
@end
#import "EditableCellStyle2.h"
@implementation EditableCellStyle2
@synthesize editField;
@synthesize lineView;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code.
editRect = CGRectMake(83, 12, self.contentView.bounds.size.width-83, 19);
editField = [[UITextField alloc] initWithFrame:editRect];
editField.font = [UIFont boldSystemFontOfSize:15];
editField.textAlignment = UITextAlignmentLeft;
editField.textColor = [UIColor blackColor];
editField.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[self.contentView addSubview:editField];
self.editField.enabled = NO;
self.editField.hidden = YES;
lineView = [[UIView alloc] initWithFrame:CGRectMake(80, 0, 1, self.contentView.bounds.size.height)];
self.lineView.backgroundColor = [UIColor lightGrayColor];
[self.contentView addSubview:lineView];
self.lineView.hidden = YES;
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state.
}
-(void)layoutSubviews
{
[super layoutSubviews]; // layouts the cell as UITableViewCellStyleValue2 would normally look like
editRect = CGRectMake(83, 12, self.contentView.frame.size.width-self.detailTextLabel.frame.origin.x-10, 19);
editField.frame = editRect;
}
- (void)willTransitionToState:(UITableViewCellStateMask)state {
[super willTransitionToState:state];
if (state & UITableViewCellStateEditingMask) {
self.detailTextLabel.hidden = YES;
self.editField.enabled = YES;
self.lineView.hidden = NO;
self.editField.hidden = NO;
}
}
- (void)didTransitionToState:(UITableViewCellStateMask)state {
[super didTransitionToState:state];
if (!(state & UITableViewCellStateEditingMask)) {
self.editField.enabled = NO;
self.editField.hidden = YES;
self.lineView.hidden = YES;
self.detailTextLabel.hidden = NO;
self.editField.text = self.detailTextLabel.text;
}
}
- (void)dealloc {
[editField release];
[lineView release];
[super dealloc];
}
@end
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// handling every section by hand since this view is essentially static. Sections 0, 1, 2, and 4 use a generic editable cell.
// Section 3 uses the multiline address cell.
static NSString *CellIdentifier = @"Cell";
EditableCellStyle2 *cell = (EditableCellStyle2 *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (indexPath.section == 0 || indexPath.section == 1 || indexPath.section == 2 || indexPath.section == 4) {
if (cell == nil) {
cell = [[[EditableCellStyle2 alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
}
}
// Configure the Odometer
if (indexPath.section == 0) {
NSArray *array = [sectionsArray objectAtIndex:indexPath.section];
NSDictionary *dictionary = [array objectAtIndex:indexPath.row];
cell.textLabel.text = @"Odometer";
cell.detailTextLabel.text = [NSString stringWithFormat:@"%@", [dictionary objectForKey:@"Odometer"]];
cell.tag = kOdometer;
cell.editField.text = cell.detailTextLabel.text;
cell.editField.placeholder = @"Odometer";
cell.editField.tag = kOdometer;
cell.editField.keyboardType = UIKeyboardTypeNumberPad;
// Create a view for the green checkmark for odometer input validation and set it as the right view.
UIImage *checkImage = [UIImage imageNamed:@"tick.png"];
UIImageView *checkImageView = [[[UIImageView alloc] initWithImage:checkImage] autorelease];
cell.editField.rightView = checkImageView;
cell.editField.rightViewMode = UITextFieldViewModeAlways;
}
return cell;
}
还有更多的,但所有的细胞都建立相同的方式。
问题是,在编辑模式下,垂直线条会正常显示。当我离开编辑模式时,当我进入正常模式时,屏幕外的任何单元仍然有垂直线(它不会被隐藏)。此外,现在我已将imageView添加为复选标记指示器,当切换模式时屏幕外的任何单元格都会获得复选标记。 (只有第0部分设置)。
我也注意到,如果我做cell.setNeedsDisplay,如果数据源已更新,文本标签和详细文本标签将不会更新。我必须做[self.tableView reloadData],它跳过任何活动的动画。
我敢肯定,这些问题与我使用自定义单元格+ dequeueReusableCellWithIdentifier相关,但我找不到确切的内容。
任何意见或推动正确的方向将不胜感激。
编辑: 不使用可重复使用的单元格似乎解决了上述问题。我仍然乐意提供关于单元代码的反馈。 我忘记了另一个可能相关或不相关的问题。我的一个单元有一个“点击查看列表”按钮。如果我在编辑模式下输入数据到单元格中,然后点击该按钮从列表中选择一些信息(它会显示一个模态表格视图),当我关闭模态视图时,所有单元格的编辑数据都恢复为它们的原始状态。当我关闭模态视图控制器时,我不会调用重新加载数据。我认为这可能通过不使用可重用单元来解决,但事实并非如此。
Yup听起来像可重复使用的单元格的问题。你的tableview有多大?大到足以证明重用细胞?如果不抛弃它们并让tableview创建其单元格的单个实例呢? – Rog 2011-02-18 09:48:32
表格视图非常小,它是核心数据条目的详细视图。也许7个细胞,上衣。我有这种奇怪的感觉,我把我的单元格中的layoutsubviews搞乱了。 – Chuck 2011-02-18 10:04:02