Reputation: 63
I have tableview with custom cell. I have textfield and image view in my table cell. When user enters some characters it should validate and change the image view based on user input. My code,
- (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;
When am entering values in textfield, condition goes finely but image view not getting updated based on input. Am getting object value for cell.Did I miss something? Kindly help me to sort out this issue. TIA.
Upvotes: 1
Views: 66
Reputation: 1780
You can get the Cell for desired row
-(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;
}
Upvotes: 1