Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom UITableViewCell (IB) only shows in selected state

I made a custom UITableViewCell in Interface Builder (Storyboard) and imported it to my project via #import CustomTableViewCell.h.

Everything works fine, but the cell is only loaded in selected state.

enter image description here

I want the cell to be loaded in every row by init.

P.S. The slider and text field connections work fine. I also made all of the IB Connections.

CustomTableViewCell.m

#import "CustomTableViewCell.h"

@implementation CustomTableViewCell

@synthesize sliderLabel, slider;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    // Initialization code
}
return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];

// Configure the view for the selected state

}

- (IBAction)getSliderValuesWithValue:(UISlider *)sender
{
sliderLabel.text = [NSString stringWithFormat:@"%i / 100", (int) roundf(sender.value)];
}

@end

Further Code

- (CustomTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Kriterium";

CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[CustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

// Configure the cell...

cell.textLabel.text = [NSString stringWithFormat:@"%@", [listOfItems objectAtIndex:indexPath.row]];

return cell;
}

P.S. If I add some Buttons etc. programmatically in the above method it works. But I want to design the rows in IB. There has to be a solution.

like image 505
DAS Avatar asked Dec 04 '11 11:12

DAS


1 Answers

Okay ... strange things happening here ... ;-) The problem was this line:

 cell.textLabel.text = [NSString stringWithFormat:@"%@", [listOfItems objectAtIndex:indexPath.row]];

Leaving it out did the trick. I had to add another UILabel to my CustomCell which I fill with text.

CONCLUSION

Filling the standard UITableViewCell.textLabel.text seems to overwrite the PrototypeCells.

... too much customization hurts. ;-)

Thanks anyway! :)

like image 67
DAS Avatar answered Oct 17 '22 19:10

DAS