使用解析填充自定义tableview

时间:2014-12-22 18:55:04

标签: parse-platform

我正在尝试使用parse中的数据填充自定义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的初学者。

1 个答案:

答案 0 :(得分:0)

以下是我为加载到UITableViewCell ...

中的自定义UITableView所做的操作

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

的NSMutableArray

CustomCell加载到UITableView只需要一种方法来设置自定义单元格中的所有值。无需在UITableView委托方法- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath中设置所有变量,因为自定义单元格通过调用setUser:来处理它。它使项目更加健壮,符合MVC范例。