Custom UITableViewCell (IB) is displayed only in the selected state

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

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

enter image description here

I want the cell to be loaded into each row using init.

PS Slider and text fields work fine. I also made all 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; } 

PS If I add some buttons, etc. Programmatically in this method, it works. But I want to create rows in IB. There must be a solution.

+8
ios xcode uitableview
source share
2 answers

Ok ... strange things are happening here ... ;-) The problem was in this line:

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

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

Conclusion

Filling out the standard UITableViewCell.textLabel.text like rewriting PrototypeCells.

... too many settings hurt .; -)

Thanks anyway!:)

+16
source share

Offering you not to go to IB. Just define these controls as a property, and in your init-initWithStyle (file CustomTableViewCell.m) initialize the UISlider with its default property:

 UISlider *tempSlider = [[UISlider alloc] initWithFrame:frame]; tempSlider.selected = NO; //define other properties as well self.slider = tempSlider; [self addSubview:self.slider]; [tempSlider release]; 

In addition, you can also set the cell selection style to none.

 cell.selectionStyle = UITableViewCellSelectionStyleNone; 
0
source share

All Articles