2014-12-22 47 views
1

我想填充自解析數據的自定義tableview。在這一點上,我可以連接我的數據和應用程序下載行,但標籤中的名稱和圖像存儲不會出現。填充自定義tableview與解析

我與引用網點

**CustomCell.h** 

    #import <UIKit/UIKit.h> 

    @interface CustomCellTableViewCell : UITableViewCell 

    @property (strong, nonatomic) IBOutlet UILabel *autor; 
    @property (strong, nonatomic) IBOutlet UILabel *titulo; 
    @property (strong, nonatomic) IBOutlet UIImageView *myImage; 

    @end 

    **CustomCell.m** 


    #import "CustomCellTableViewCell.h" 

    @implementation CustomCellTableViewCell 

    @synthesize consola = _autor; 
    @synthesize titulo = _titulo; 
    @synthesize myImage = _myImage; 


    - (void)awakeFromNib { 
     // Initialization code 
    } 

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

     // Configure the view for the selected state 
    } 

    @end 

    **ViewController.h** 

    #import <UIKit/UIKit.h> 
    #import <Parse/Parse.h> 
    #import <ParseUI/ParseUI.h> 


    @interface ViewController : PFQueryTableViewController 


    @end 

    **ViewContoller.m** 

    #import "ViewController.h" 
    #import "CustomCell.h" 


    @interface ViewController() 



    @end 

    @implementation ViewController 

    - (void)viewDidLoad { 
     [super viewDidLoad]; 
    } 


    - (void)didReceiveMemoryWarning { 
     [super didReceiveMemoryWarning]; 
     // Dispose of any resources that can be recreated. 
    } 

    #pragma mark - Table view data source 


    - (id)initWithCoder:(NSCoder *)aCoder 
    { 
     self = [super initWithCoder:aCoder]; 
     if (self) { 
      // The className to query on 
      self.parseClassName = @"Libros"; 

      // The key of the PFObject to display in the label of the default cell style 
      self.textKey = @"author"; 

      // Whether the built-in pull-to-refresh is enabled 
      self.pullToRefreshEnabled = YES; 

      // Whether the built-in pagination is enabled 
      self.paginationEnabled = NO; 
     } 
     return self; 
    } 

    - (PFQuery *)queryForTable 
    { 
     PFQuery *query = [PFQuery queryWithClassName:self.parseClassName]; 

     return query; 
    } 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object 
    { 
     static NSString *simpleTableIdentifier = @"Cell"; 

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

     // Configure the cell 
     PFFile *thumbnail = [object objectForKey:@"imagen"]; 
     PFImageView *thumbnailImageView = (PFImageView*)[cell viewWithTag:100]; 


     UILabel *titulo = (UILabel*) [cell viewWithTag:101]; 
     titulo.text = [object objectForKey:@"titulo"]; 

     UILabel *autor = (UILabel*) [cell viewWithTag:102]; 
     consola.text = [object objectForKey:@"autor"]; 

     return cell; 
    } 

    @end 

我怎麼能顯示我在標籤和圖像數據連接與一個圖像和兩個標籤自定義單元格?

我是Xcode的初學者。

+0

你不應該加載一個'UITableViewCell'到你的'UITableView'中,你應該加載你自定義的'UITableViewCell','CustomCellTableViewCell'。然後訪問它的屬性,你會......你不需要獲得一個指向標籤或圖像的指針,只需在命名它們時訪問屬性......另外,你應該有一個'PFImageView'公共屬性,而不是你自定義的'UITableViewCell'中的'UIImageView' – jsetting32

回答

0

這裏是我做了裝載到UITableView定製UITableViewCell ...

CBRearViewProfileCell.h

@class CBProfileImageView; 

@protocol CBRearViewProfileCellDelegate; 

@interface CBRearViewProfileCell : UITableViewCell 
@property (nonatomic, strong) id<CBRearViewProfileCellDelegate> delegate; 
@property (nonatomic, strong) PFUser *user; 
- (void)setUser:(PFUser *)user; 
+ (CGFloat)heightForCell; 
@end 

@protocol CBRearViewProfileCellDelegate <NSObject> 
@optional 
- (void)cell:(CBRearViewProfileCell *)cellView didTapUserButton:(PFUser *)aUser; 
@end 

CBRearViewProfileCell.h

#import "CBRearViewProfileCell.h" 
#import "CBProfileImageView.h" 
#import "CBConstants.h" 

@interface CBRearViewProfileCell() 
@property (nonatomic, strong) CBProfileImageView *avatarImageView; 
@property (nonatomic, strong) UILabel *nameLabel; 
@end 

@implementation CBRearViewProfileCell 
@synthesize avatarImageView; 
@synthesize nameLabel; 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 

     self.selectionStyle = UITableViewCellSelectionStyleGray; 

     self.avatarImageView = [[CBProfileImageView alloc] init]; 
     self.avatarImageView.frame = CGRectMake(14.5f, 6.0f, 32.0f, 32.0f); 
     [self.avatarImageView.profileButton addTarget:self action:@selector(didTapUserButtonAction:) forControlEvents:UIControlEventTouchUpInside]; 
     [self.contentView addSubview:self.avatarImageView]; 

     self.nameLabel = [[UILabel alloc] init]; 
     self.nameLabel.backgroundColor = [UIColor clearColor]; 
     self.nameLabel.font = [UIFont fontWithName:kCBFontBold size:16.0f]; 
     self.nameLabel.textColor = [UIColor whiteColor]; 
     self.nameLabel.lineBreakMode = NSLineBreakByTruncatingTail; 
     [self.contentView addSubview:self.nameLabel]; 
    } 
    return self; 
} 

- (void)setUser:(PFUser *)aUser { 
    [avatarImageView setFile:[aUser objectForKey:kCBUserProfilePicSmallKey]]; 
    [nameLabel setText:[aUser objectForKey:kCBUserDisplayNameKey]]; 
    [nameLabel setFrame:CGRectMake(60.0f, 12.5f, self.bounds.size.width/2.0f, 20.0f)]; 
} 

+ (CGFloat)heightForCell { 
    return 44.0f; 
} 

/* Inform delegate that a user image or name was tapped */ 
- (void)didTapUserButtonAction:(id)sender { 
    if (self.delegate && [self.delegate respondsToSelector:@selector(cell:didTapUserButton:)]) { 
     [self.delegate cell:self didTapUserButton:self.user]; 
    } 
} 

@end 

MyTableViewController.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *PFCellIdentifier = @"parseCell"; 
    CBRearViewProfileCell *cell = [tableView dequeueReusableCellWithIdentifier:PFCellIdentifier]; 

    if (!cell) { 
     cell = [[CBRearViewProfileCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:PFCellIdentifier]; 
     [[cell textLabel] setFont:[UIFont fontWithName:kCRFont size:14.0f]]; 
     [[cell detailTextLabel] setFont:[UIFont fontWithName:kCRFont size:14.0f]]; 
     [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator]; 
    } 

    [cell setUser:[self.objects objectAtIndex:indexPath.row]]; 

    return cell; 
} 

self.objects是含有PFObjects

加載CustomCellUITableView一個的NSMutableArray只需要一個方法來定製單元內設置的所有的值。由於定製單元通過調用setUser:來處理它,因此無需在UITableView委託方法- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中設置所有變量。它使項目更加健壯並符合MVC範例。