В моих приложениях строки добавляются к моему TableView
с другого вида. Когда пользователь добавляет строки, пользователь возвращается обратно к TableView
. Проблема в том, что ранее введенный текст больше не отображается.Значение UITextfield успешно обновлено, но не показано в таблице
Я могу загрузить его с помощью NSMutableDictionary
, но пользователь не может его просмотреть. Любые идеи о том, что я должен делать? какой код я должен добавить и где его добавить? Большое спасибо!
Вот код от tableview
метода. Я думаю, что исправление пойдет где-нибудь.
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
static NSString *CellIdentifier = @"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.wtf = [[UITextField alloc]init];
NSUInteger count =0;
for (NSMutableDictionary *search in dataForAllRows){ //this just helps me pull the right data out of an array of NSMutableDictionary's
if ([search valueForKey:@"indexSection"] == [NSNumber numberWithInteger:(indexPath.section -1)]) {
if ([search valueForKey:@"indexRow"] == [NSNumber numberWithInteger:indexPath.row]) {
NSMutableDictionary *match = [dataForAllRows objectAtIndex:count];
[cell.wtf setText:[match objectForKey:@"wtf"]];
NSLog(@"%@",cell.wtf.text); // this outputs the correct value in the command line
}
}
count++;
}
}
}
Вот код для моего CustomCell.m
#import "CustomCell.h"
@implementation CustomCell
@synthesize wtf, cellPath;
- (void)awakeFromNib {
// Initialization code
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(void)layoutSubviews{
wtf = [[UITextField alloc] initWithFrame:CGRectMake(7, 3, 65, self.contentView.bounds.size.height-6)];
self.wtf.delegate = self;
[wtf setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
[wtf setAutocorrectionType:UITextAutocorrectionTypeNo];
[wtf setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[wtf setBorderStyle:UITextBorderStyleRoundedRect];
wtf.textAlignment = NSTextAlignmentCenter;
wtf.keyboardType = UIKeyboardTypeNumberPad; //
[wtf setAutocapitalizationType:UITextAutocapitalizationTypeWords];
[wtf setPlaceholder:@"enter"];
[self.contentView addSubview:wtf];
}
Два улучшения сделают вашу жизнь намного проще: (1) Является ли клетка определена в раскадровке/И.Б.? Если нет, сделайте это, (2) простое преобразование вашего источника данных (dataForAllRows) позволит прояснить код и избежать полного поиска данных каждый раз, когда строка прокручивается в поле зрения. – danh
@ danh Нет, моя ячейка не определена в сюжетной доске/ib. Я посмотрю, как это сделать. Это корень моей проблемы? Извините, если некоторые из моих вопросов являются основными. Я новичок в этом. – njyulan
Не могли бы вы добавить 'cell.wtf.backgroundColor = [UIColor greenColor];' в свой код, чтобы убедиться, что текстовое поле добавляется в ячейку правильно. А также поделитесь кодом, в котором вы добавляете UITextfield в ячейку. – williamb