定制单元集成到工作单元中不起作用

时间:2015-08-30 22:37:56

标签: ios objective-c uitableview

我有正常单元格和工作成功的项目,但我想用自定义单元格更改它我将Cell.h和Cell.m文件添加到我的项目中,我需要将自定义单元格集成到我的工作单元格中。最后我想显示详细视图标题描述和图像(我添加了标题代码)我的代码在

Cell.h

#import <UIKit/UIKit.h>

@interface Cell : UITableViewCell

@property (nonatomic, weak) IBOutlet UIImageView *imaj;

@property (nonatomic, weak) IBOutlet UILabel *descriptionLabel;

@property (nonatomic, weak) IBOutlet UILabel *titleLabel;

@end

Cell.m

#import "Cell.h"

@implementation Cell

@synthesize imaj = _imaj;
@synthesize descriptionLabel = _descriptionLabel;
@synthesize titleLabel = _titleLabel;


@end

ViewController.m

#import "MasterViewController.h"
#import "DetailViewController.h"
#import "SDWebImage/UIImageView+WebCache.h"
#import "MBProgressHUD.h"
#import "Cell.h"



static NSString *const kConsumerKey = @"a1SNULSPtp4eLQTsTXKKSgXkYB5H4CMFXmleFvqE";

@interface MasterViewController () <UISearchBarDelegate, UISearchDisplayDelegate,MBProgressHUDDelegate>{

    MBProgressHUD *HUD;

}

@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, assign) NSInteger totalPages;
@property (nonatomic, assign) NSInteger totalItems;
@property (nonatomic, assign) NSInteger maxPages;

@property (nonatomic, strong) NSMutableArray *activePhotos;
@property (strong, nonatomic) NSMutableArray *staticDataSource;
@property (nonatomic, strong) NSMutableArray *searchResults;

@property (strong, nonatomic) IBOutlet UISearchBar *searchBar;
@property (strong, nonatomic) IBOutlet UITableView *tableView;


@end

@implementation MasterViewController


- (void)viewDidLoad
{
    [super viewDidLoad];

    self.activePhotos = [[NSMutableArray alloc] init];
    self.searchResults = [[NSMutableArray alloc] init];
    self.staticDataSource = [[NSMutableArray alloc] init];
}

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

    [self loadPhotos:self.currentPage];
}


#pragma mark - Table View

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    if (self.currentPage == self.maxPages
//        || self.currentPage == self.totalPages
//        || self.currentPage == self.totalPages
//        || self.totalItems == self.photos.count) {
//        return self.photos.count;
//    } else if (self.tableView == self.searchDisplayController.searchResultsTableView){
//        return [self.searchResults count];
//        
//    }
//    return self.photos.count + 1;
    return  self.activePhotos.count + 1;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.currentPage != self.maxPages && indexPath.row == [self.staticDataSource count] - 1 ) {
        [self loadPhotos:++self.currentPage];
    }
}

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


    if (indexPath.row == [self.activePhotos count]) {
        cell = [self.tableView dequeueReusableCellWithIdentifier:@"LoadingCell" forIndexPath:indexPath];


        HUD = [[MBProgressHUD alloc] initWithView:self.navigationController.view];
        [self.navigationController.view addSubview:HUD];

        HUD.delegate = self;
        HUD.labelText = @"Loading";

        [HUD showWhileExecuting:@selector(myTask) onTarget:self withObject:nil animated:YES];

    } else {
        NSDictionary *photoItem = self.activePhotos[indexPath.row];
        cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
        //            cell.textLabel.text = [self.searchResults[indexPath.row] valueForKey:@"name"];
        //        } else {
        //            NSDictionary *photoItem = self.photos[indexPath.row];




        cell.textLabel.text = [photoItem objectForKey:@"name"];



        if (![[photoItem objectForKey:@"description"] isEqual:[NSNull null]]) {
            cell.detailTextLabel.text = [photoItem objectForKey:@"description"];
        }
        [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[photoItem objectForKey:@"image_url"] ] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            if (error) {
                NSLog(@"Error occured : %@", [error description]);
            }
        }];

    }

//    NSLog(@"%@",self.searchResults);

    return cell;
}

- (void)myTask {
    // Do something usefull in here instead of sleeping ...
    sleep(1.5);
}

#pragma mark UISearchDisplay delegate

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
//    [self.searchResults removeAllObjects];

    NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF.name contains[c] %@", searchText];

    self.activePhotos = [NSMutableArray arrayWithArray:[self.staticDataSource filteredArrayUsingPredicate:resultPredicate]];
    //[self.tableData filteredArrayUsingPredicate:resultPredicate];
}

-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
                                      objectAtIndex:[self.searchDisplayController.searchBar
                                                     selectedScopeButtonIndex]]];

    return YES;
}

- (void)loadPhotos:(NSInteger)page
{
    NSString *apiURL = [NSString stringWithFormat:@"https://api.500px.com/v1/photos?feature=editors&page=%ld&consumer_key=%@",(long)page,kConsumerKey];

    NSURLSession *session = [NSURLSession sharedSession];
    [[session dataTaskWithURL:[NSURL URLWithString:apiURL]
            completionHandler:^(NSData *data,
                                NSURLResponse *response,
                                NSError *error) {

                if (!error) {

                    NSError *jsonError = nil;
                    NSMutableDictionary *jsonObject = (NSMutableDictionary *)[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&jsonError];

                    NSLog(@"%@",jsonObject);

                    [self.staticDataSource addObjectsFromArray:[jsonObject objectForKey:@"photos"]];

                    self.currentPage = [[jsonObject objectForKey:@"current_page"] integerValue];
                    self.totalPages  = [[jsonObject objectForKey:@"total_pages"] integerValue];
                    self.totalItems  = [[jsonObject objectForKey:@"total_items"] integerValue];

                    self.activePhotos = self.staticDataSource;

                    dispatch_async(dispatch_get_main_queue(), ^{
                        [self.tableView reloadData];
                    });
                }
            }] resume];
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    DetailViewController *vc = segue.destinationViewController;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
    vc.StoreList = [self.activePhotos objectAtIndex:indexPath.row];



}


@end

我也在这里上传了工作项目 http://www.filedropper.com/needcustomcell

1 个答案:

答案 0 :(得分:0)

在你的

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Cell *cell;
// rest of your code 
}

会这样做。您已经使用了UITableViewCell,您需要将其替换为自定义单元类。