Parse platform 使用parse填充自定义tableview
我正在尝试用parse中的数据填充自定义tableview。此时,我可以连接我的数据,应用程序下载行,但标签和图像存储中的名称不会出现 我有一个自定义单元格,其中一个图像和两个标签与参考插座相连Parse platform 使用parse填充自定义tableview,parse-platform,Parse Platform,我正在尝试用parse中的数据填充自定义tableview。此时,我可以连接我的数据,应用程序下载行,但标签和图像存储中的名称不会出现 我有一个自定义单元格,其中一个图像和两个标签与参考插座相连 **CustomCell.h** #import <UIKit/UIKit.h> @interface CustomCellTableViewCell : UITableViewCell @property (strong, nonatomic) IBOut
**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
**CustomCell.h**
#进口
@接口CustomCellTableViewCell:UITableViewCell
@属性(强,非原子)IBUILabel*autor;
@性质(强,非原子)IBUILabel*titulo;
@属性(强,非原子)IBUIImageView*myImage;
@结束
**CustomCell.m**
#导入“CustomCellTableViewCell.h”
@CustomCellTableViewCell的实现
@综合控制台=_autor;
@合成titulo=_titulo;
@合成myImage=\u myImage;
-(无效)从NIB中唤醒{
//初始化代码
}
-(无效)设置选定:(BOOL)选定动画:(BOOL)动画{
[超级设置选定:选定动画:动画];
//为所选状态配置视图
}
@结束
**ViewController.h**
#进口
#进口
#进口
@界面ViewController:PFQueryTableViewController
@结束
**ViewContoller.m**
#导入“ViewController.h”
#导入“CustomCell.h”
@界面视图控制器()
@结束
@实现视图控制器
-(无效)viewDidLoad{
[超级视图下载];
}
-(无效)未收到记忆警告{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
#pragma标记-表视图数据源
-(id)initWithCoder:(NSCoder*)一个代码
{
self=[super initWithCoder:aCoder];
如果(自我){
//要查询的类名
self.parseClassName=@“Libros”;
//要在默认单元格样式的标签中显示的PFObject的键
self.textKey=@“作者”;
//是否启用了内置的拉入刷新功能
self.pullToRefreshanbled=是;
//是否启用内置分页
self.paginationEnabled=否;
}
回归自我;
}
-(PFQuery*)queryForTable
{
PFQuery*query=[pfqueryquerywithclassname:self.parseClassName];
返回查询;
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath对象:(PFObject*)对象
{
静态NSString*simpleTableIdentifier=@“单元格”;
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:simpleTableIdentifier];
}
//配置单元格
PFFile*缩略图=[object objectForKey:@“imagen”];
PFImageView*thumbnailImageView=(PFImageView*)[带标记的单元格视图:100];
UILabel*titulo=(UILabel*)[带标记的单元格视图:101];
titulo.text=[objectobjectforkey:@“titulo”];
UILabel*autor=(UILabel*)[带标记的单元格视图:102];
consola.text=[objectobjectforkey:@“autor”];
返回单元;
}
@结束
如何在标签和图像中显示数据
我是Xcode的初学者 以下是我为加载到
UITableView
中的自定义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
#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
将CustomCell
加载到UITableView
只需一种方法即可设置自定义单元格中的所有值。无需在UITableView委托方法-(UITableViewCell*)tableView:(UITableView*)tableView CellforRowatineXpath:(NSIndexPath*)indexPath中设置所有变量,因为自定义单元格通过调用setUser:
来处理它。它使项目更加健壮,并符合MVC范例。您不应该将UITableViewCell
加载到UITableView
中,而应该加载自定义的UITableViewCell
,CustomCellTableViewCell
。然后访问它的属性,就像你想。。。您不需要获取指向标签或图像的指针,只需访问您命名的属性即可。。。此外,在自定义UITableViewCell