【发布时间】:2017-09-06 09:20:51
【问题描述】:
我有自定义单元格的表格视图。我的表格单元格中有文本字段和图像视图。当用户输入一些字符时,它应该根据用户输入验证和更改图像视图。我的代码,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"newsTableCell";
cell = (NewsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[NewsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.answerTextfield.delegate = self;
if (indexPath.row == 0) {
cell.newsTitleLabel.text = @"News 1";
cell.lengthImageView.tag = 300;
}
else if (indexPath.row == 1) {
cell.newsTitleLabel.text = @"News 2";
cell.lengthImageView.tag = 301;
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([NewsValidation charactersInString:string checkFor:@"ValidationString"]) {
if ([string length] + [textField.text length] > 1 && [textField.text length] < 36) {
cell.lengthImageView.image = [UIImage imageNamed:@"Right.png"];
return YES;
}
} else {
cell.lengthImageView.image = [UIImage imageNamed:@"Wrong.png"];
return NO;
}
return YES;
}
NewsTableViewCell.h
@interface NewsTableViewCell : UITableViewCell {
IBOutlet UIImageView *lengthImageView;
}
@property (nonatomic, retain)IBOutlet UIImageView *lengthImageView;
在文本字段中输入值时,条件正常,但图像视图未根据输入更新。正在获取单元格的对象值。我错过了什么吗?请帮我解决这个问题。 TIA。
【问题讨论】:
-
我建议您将您的 tableview 单元格作为您的 textField 的委托,并为您的自定义 tableviewcell 类实现委托方法。然后根据需要更改图像。
-
您要更改所有
UITableViewCell单元格的图像视图吗?
标签: ios objective-c uitableview uiimageview tableview