【问题标题】:change imageview in custom tablecell在自定义表格单元中更改图像视图
【发布时间】: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


【解决方案1】:

您可以获得所需行的单元格

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
         NSIndexPath *indexPath = [NSIndexPath indexPathForRow:4 inSection:0]; 
         NewsTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
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;
}

【讨论】:

    【解决方案2】:

    尝试在文本字段中输入文本时重新加载表格视图单元格。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多