Custom UITableViewCell from xib isn't displaying properly
I've created custom UITableCells a bunch of times and I've never run into this problem, so I'm hoping you can help me find the thing I've missed or messed up. When I run my app, the cells in my table view appear to be standard cells with Default style.
I have SettingsTableCell which is a subclass of UITableViewCell. I have a SettingsTableCell.xib which contains a UITableViewCell and inside that are a couple labels and a textfield. I've set the class type in the xib to be SettingsTableCell and the File's Owner of the xib to my table controller.
My SettingsTableController has an IBOutlet property named tableCell. My cellForRowAtIndexPath contains the following code to load my table view xib and assign it to my table controller's tableCell property:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CellSettings";
SettingsTableCell *cell = (SettingsTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
开发者_如何转开发 if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"SettingsTableCell" owner:self options:nil];
cell = self.tableCell;
self.tableCell = nil;
NSLog(@"cell=%@", cell);
}
// Configure the cell...
NSArray *sections = [self.settingsDictionary objectForKey:KEY_GROUPS];
NSDictionary *sectionInfo = [sections objectAtIndex:[indexPath section]];
NSArray *itemsInSection = [sectionInfo objectForKey:KEY_FIELDS];
NSDictionary *item = [itemsInSection objectAtIndex:[indexPath row]];
cell.textLabel.text = [item objectForKey:KEY_LABEL_NAME];
cell.labelName.text = [item objectForKey:KEY_LABEL_NAME];
cell.labelUnitsType.text = [item objectForKey:KEY_LABEL_UNITS];
return cell;
}
This is what my xib set up looks like in IB:
When I run my app, the table displays as if all of the cells are standard Default style cells though:
The seriously weird part is though... if I tap on the area of the cell where the textfield SHOULD be, the keyboard does come up! The textfield isn't visible, there's no cursor or anything like that... but it does respond. The visible UILabel is obviously not the UILabel from my xib though because the label in my xib is right justified and the one showing in the app is left justified.
I'm incredibly confused about how this is happening. Any help is appreciated.
EDIT: Here is the code for my SettingsTableCell class:
@interface SettingsTableCell : UITableViewCell {
UILabel *labelName;
UILabel *labelUnitsType;
UITextField *field;
}
@property (nonatomic, retain) IBOutlet UILabel *labelName;
@property (nonatomic, retain) IBOutlet UILabel *labelUnitsType;
@property (nonatomic, retain) IBOutlet UITextField *field;
@end
#import "SettingsTableCell.h"
@implementation SettingsTableCell
@synthesize labelName;
@synthesize labelUnitsType;
@synthesize field;
- (void)dealloc {
[labelName release];
labelName = nil;
[labelUnitsType release];
labelUnitsType = nil;
[field release];
field = nil;
[super dealloc];
}
@end
I don't know why, but I do know that strange things happen while saving the cell in instance variables.
Have you tried loading the cell directly in cellForRowAtIndexPath
?
if (cell == nil) {
topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MyNibName" owner:nil options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
cell = currentObject;
break;
}
}
}
Your complete code for cellForRowAtIndexPath
and SettingsTableCell.h/m would be of help.
My first thought (probably wrong!) is that this is a z order issue and that the cells default label is being displayed on top of your text editing control. Hence not being able to see it. I'd guess that it still responds because the touch is being passed through by the label.
Just a guess :-)
精彩评论