2011-12-04 28 views
8

我在Interface Builder(故事板)做了一個自定義的UITableViewCell,並通過#import CustomTableViewCell.h它導入到我的項目。自定義的UITableViewCell(IB)只顯示處於選擇狀態

一切工作正常,但細胞只在選中狀態加載。

enter image description here

我想由init每一行中要加載的細胞。

P.S.滑塊和文本字段連接正常工作。我也做了所有的IB連接。

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; 
} 

附:如果我在上面的方法中編程添加一些按鈕等,它的工作原理。但我想要在IB中設計行。必須有一個解決方案。

+0

您可以發佈與填充行的UITableView時使用您的cellForRowAtIndexPath方法的代碼? – reddersky

+0

我更新了我的答案。 – DAS

+0

您是否在單獨的筆尖中設計了該單元格,或者在故事板中的表格視圖中將其設計爲原型? – jrturton

回答

16

好吧......奇怪的事情發生在這裏... ;-)問題是這一行:

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

離開它的伎倆。我不得不添加另一個UILabel到我填寫文本的CustomCell。

結論

灌裝標準UITableViewCell.textLabel.text似乎覆蓋PrototypeCells。

...太多的定製傷害。 ;-)

無論如何還要謝謝! :)

+0

您的意思是在我的解決方案還是一般?再次感謝你jr! – DAS

+0

我的意思是一般 - 如果您使用默認的單元格樣式,然後添加子視圖,這可能是要注意的東西。 – jrturton

+0

好的,謝謝。這只是因爲我需要一堆不同的單元格樣式。我更容易在IB中看到它們。 :) – DAS

0

建議你不要去IB。只是定義這些控件屬性,並以你的初始化方法 - initWithStyle(CustomTableViewCell.m文件)與它的默認屬性初始化UISlider:

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

除此之外,您還可以設置單元格選擇樣式無法比擬的。

cell.selectionStyle = UITableViewCellSelectionStyleNone; 
+0

謝謝。但它基本上必須像[this(click)](http://www.raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1)一樣工作。每個人都使用這條線,它不會改變我的問題:'UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@「PlayerCell」];' – DAS

相關問題