2013-07-17 23 views
6

Tôi đã tạo một ô tùy chỉnh FeatureCell có 5 hình ảnh trong hàng sẽ được gọi trong chế độ xem chính nhưng khi tôi gọi nó là hàng trống. Vì vậy, xin vui lòng nơi có thể là vấn đề của tôi?Gọi hai ô tùy chỉnh khác nhau trong một vấn đề UITableView

Tôi đã googled về ô tùy chỉnh và tôi đã sử dụng cách mà tôi phải sử dụng trong mã bên dưới nhưng không có gì xảy ra.

Đây là FeatureCell.h tôi

@interface FeatureCell : UITableViewCell{ 

    IBOutlet UIImageView *img1; 
    IBOutlet UIImageView *img2; 
} 

@property (nonatomic, retain) UIImageView *img1; 
@property (nonatomic, retain) UIImageView *img2; 
@end 

Đây là FeatureCell.m tôi

@implementation FeatureCell 

@synthesize img,img1,img2,img3,img4; 
@end 

Đây là view-controller của tôi .m

- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{ 

return 2; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 

if (section == 0) { 
    return [objectCollection count]; 
} 
else{ 
    return 1; 
    } 
}  

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

static NSString* cellIdentifier1 = @"FeatureCell"; 

FeatureCell *cell1 = (FeatureCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier1]; 


if (cell1 == nil) 
{ 
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellIdentifier1 owner:nil options:nil]; 

    cell1 = (FeatureCell*)[nib objectAtIndex:0]; 
} 

static NSString *CellIdentifier = @"Cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 


if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 
            reuseIdentifier:CellIdentifier]; 

} 


if ([indexPath section] == 0) { 

    containerObject = [objectCollection objectAtIndex:indexPath.row]; 

    [[cell textLabel] setText:containerObject.store]; 

    cell.selectionStyle = UITableViewCellSelectionStyleNone; 

} 
    else{ 

    cell1.img1.image = [UIImage imageNamed:@"shower.png"]; 
    cell1.img2.image = [UIImage imageNamed:@"parking.png"]; 

    } 
     return cell; 
} 
+0

Bạn kết nối các cửa hàng? – Wain

+0

Đây không phải là cách chính xác để xử lý 2 loại ô khác nhau. Xem câu trả lời của tôi ở đây: http://stackoverflow.com/questions/17711452/change-uicollectionviewcell-content-and-nib-layout-based-on-data/17711644#17711644. Điều này đề cập đến lượt xem bộ sưu tập, nhưng nó giống với chế độ xem bảng. – rdelmar

+0

@Wain ý của bạn là gì? –

Trả lời

13

Bạn cần phải làm một cái gì đó như thế này:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (indexPath.section == 0) { 
     static NSString *CellIdentifier = @"Cell"; 

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

     containerObject = [objectCollection objectAtIndex:indexPath.row]; 
     [[cell textLabel] setText:containerObject.store]; 
     cell.selectionStyle = UITableViewCellSelectionStyleNone; 

     return cell; 
    } else { 
     static NSString* cellIdentifier1 = @"FeatureCell"; 

     FeatureCell *cell1 = (FeatureCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier1]; 
     if (cell1 == nil) { 
      NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellIdentifier1 owner:nil options:nil]; 
      cell1 = (FeatureCell*)[nib objectAtIndex:0]; 
     } 

     cell1.img1.image = [UIImage imageNamed:@"shower.png"]; 
     cell1.img2.image = [UIImage imageNamed:@"parking.png"]; 

     return cell1; 
    } 
} 

tôi có thể có tình trạng ngược if để kiểm tra xem đôi.

+0

cellForRowAtIndexPath cần trả lại phiên bản UITableViewCell. Mã của bạn sẽ ném một lỗi, bởi vì bạn không trả lại bất cứ điều gì bên ngoài câu lệnh if-else. – Sebyddd

+0

@Sebyddd không sao. – rmaddy

+0

Chỉ cần thử nó, 'Kiểm soát có thể đạt đến kết thúc của chức năng không void.' :) – Sebyddd

0

thi này:

@implementation FeatureCell 
@synthesize img,img1,img2,img3,img4; 
@end 

Kết quả trong ô có một số phương thức truy cập nhưng không có phiên bản được khởi tạo. Bạn cần triển khai phương thức init để tạo các phiên bản hoặc kết nối các biến (mà bạn xác định là các cửa hàng) cho tệp XIB được liên kết. Đọc this guide.

Tôi cũng đồng ý với các nhận xét về cấu trúc cách bạn tạo các phiên bản ô khác nhau của mình. Nó rất vô tổ chức và dường như bạn đang trộn lẫn các tài liệu tham khảo. Bạn nên phân chia mọi thứ ra thành các phương thức khác nhau hoặc ít nhất là đặt mã để tạo các ô bên trong các câu lệnh if để bạn không thể đánh máy các tên mẫu ô.

0
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

{ if (indexPath.row == 0) // không indexPath.section ... cố định vấn đề của tôi

{ 
    static NSString *CellIdentifier = @"MenuHeadingCustomCell"; 

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

    return cell; 
} 





else 
    { 
     static NSString* cellIdentifier1 = @"LevelViewCell"; 
     LevelViewCell *cell1 =[tableView dequeueReusableCellWithIdentifier:cellIdentifier1]; 
     if (cell1 ==nil) 
     { 
      cell1 = [[LevelViewCell alloc]initWithStyle:UITableViewCellStyleDefault 
             reuseIdentifier:cellIdentifier1]; 

     // row counts which dictionary entry to load: 
     _Dictionary=[_array objectAtIndex:indexPath.row]; 

     cell1.LevelTitleText.text =[NSString stringWithFormat:@"%@",[_Dictionary objectForKey:@"LevelLabelText"]]; 
     cell1.LevelSubText.text =[NSString stringWithFormat:@"%@",[_Dictionary objectForKey:@"LevelLabelSubText"]]; 
     cell1.LevelThumb.image =[UIImage imageNamed:[_Dictionary objectForKey:@"LevelLabelThumb"]]; 

    } 
    return cell1; 

} 
Các vấn đề liên quan