2014-01-29 82 views
0

我試圖創建和我的表視圖使用自定義的UITableView細胞,但它的作用時髦。當我運行應用程序的表看起來像這樣:自定義的UITableViewCell不正確顯示

enter image description here

這是正確的。然而,當我選擇一個單元格,視圖轉移到這一點:

enter image description here

據我所知,似乎選擇小區後,該小區視圖轉換到默認的UITableViewCell佈局與我的明星形象它背後......如果我繼續選擇每個小區我得到這個:

enter image description here

正如你所看到的,明星形象(我的自定義視圖)只顯示了(儘管扭曲),當我選擇單元格再次。

我已經通過Apple's tutorial試圖使自定義單元格,所以我知道我做的一切我應該,但沒有人在計算器上其他人也有類似的問題,當走了......

這裏我的項目的代碼。

的TableView控制器部首

// 
// TableViewController.h 
// CustomCell 
// 
// Created by Jordan Gardner on 1/29/14. 
// Copyright (c) 2014 Jordan Gardner. All rights reserved. 
// 

#import <UIKit/UIKit.h> 

@interface TableViewController : UITableViewController 

@end 

的TableView控制器實現

// 
// TableViewController.m 
// CustomCell 
// 
// Created by Jordan Gardner on 1/29/14. 
// Copyright (c) 2014 Jordan Gardner. All rights reserved. 
// 

#import "TableViewController.h" 
#import "CustomCell.h" 

@interface TableViewController() 

@end 

@implementation TableViewController 

#pragma mark - Table view methods 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return 5; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSString *identifier = @"Cell"; 

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 

    // Configure cell... 
    cell.textLabel.text = [NSString stringWithFormat:@"Item %@", [NSNumber numberWithInteger:indexPath.row]]; 
    cell.imageView.image = [UIImage imageNamed:@"icon_folder.png"]; 

    return cell; 
} 

@end 

CustomCell部首

// 
// CustomCell.h 
// CustomCell 
// 
// Created by Jordan Gardner on 1/29/14. 
// Copyright (c) 2014 Jordan Gardner. All rights reserved. 
// 

#import <UIKit/UIKit.h> 

@interface CustomCell : UITableViewCell 

@property (weak, nonatomic) IBOutlet UIImageView *imageView; 
@property (weak, nonatomic) IBOutlet UIButton *favoriteButton; 
@property (weak, nonatomic) IBOutlet UILabel *textLabel; 

@end 

CustomCell實施

// 
// CustomCell.m 
// CustomCell 
// 
// Created by Jordan Gardner on 1/29/14. 
// Copyright (c) 2014 Jordan Gardner. All rights reserved. 
// 

#import "CustomCell.h" 

@implementation CustomCell 

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

@end 

那麼,有什麼建議,我怎麼能防止從意見行事如此扭曲,將不勝感激。提前致謝。

+2

我建議你在自定義單元格中使用不同的名稱,而不是使用默認的'textLabel'和'imageView'。有時候,他們會發生衝突。 – n00bProgrammer

+0

而錢去@ n00bProgrammer – ngoue

+0

@ n00bProgrammer可能是對的,我首先將textLabel改爲別的。其次,你在使用自動佈局嗎?如果是這樣,請檢查您是否沒有模糊的佈局。 – ansible

回答

0

正如@ n​​00bProgrammer在問題的評論中提到,我是使用像textLabelimageView的名字是用默認值相互矛盾,因此造成的問題。

-1
  • 佈局在您的UITableView
  • 的自定義單元格選擇自定義單元格,選擇身份檢查。爲新單元添加自定義的UitableViewCell類。
  • 選擇屬性檢查器並根據您的代碼添加唯一標識符。
  • 將單元格上的控件與自定義類建立連接。
  • 加載tableview時,訪問下面的自定義單元格上的控件。

    MSCMoreOptionTableViewCell *cell = (MSCMoreOptionTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    
    
    if (cell == nil){ 
        cell = [[MSCMoreOptionTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault 
                 reuseIdentifier:@"cell"]; 
    } 
    
    
    cell.label_whatever.text = @"whatever"; 
    
+0

我很欣賞這種努力,但您可能已經太快地閱讀了這個問題......我已經完成了所有這些。 – ngoue

0

這裏是解決方案。所有你需要做的是在的cellForRowAtIndexPath添加下面的代碼。

cell.contentView.frame = cell.bounds 
cell.contentView.autoresizingMask = [.FlexibleLeftMargin, 
            .FlexibleWidth, 
            .FlexibleRightMargin, 
            .FlexibleTopMargin, 
            .FlexibleHeight, 
            .FlexibleBottomMargin] 
+0

正如問題評論中提到的那樣,@ n00bProgrammer正確地指出我正在使用默認名稱 - 「textLabel」和「imageView」 - 這是導致不正常行爲的原因。 – ngoue

相關問題