I want to images display for cell. but this code display, only white tableview.Why? Please tell me. (I use not storyboard.)
TableCell.h
#import <UIKit/UIKit.h>
@interface TableCell : UITableViewCell <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,retain) UITableView *tableCellView;
@property (nonatomic,retain) NSArray *cellArray;
-(NSString *)reuseIdentifier;
@end
TableCell.m
#import "TableCell.h"
@implementation TableCell
@synthesize tableCellView;
@synthesize cellArray;
-(NSString *)reuseIdentifier
{
return @"cell";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [cellArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.tableCellView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
for (UIImageView *view in cell.subviews)
{
[view removeFromSuperview];
}
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
imageView.image = [UIImage imageNamed:[cellArray objectAtIndex:indexPath.row]];
imageView.contentMode = UIViewContentModeCenter; // 画像サイズを合わせて貼る
CGAffineTransform rotateImage = CGAffineTransformMakeRotation(M_PI_2);
imageView.transform = rotateImage;
[cell addSubview:imageView];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 220;
}
@end
TableViewController.h
#import <UIKit/UIKit.h>
@class TableCell;
@interface TableViewController : UIViewController <UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) TableCell *tableViewCell;
@property (nonatomic, retain) NSArray *titlesArray;
@property (nonatomic, retain) NSArray *peopleArray;
@property (nonatomic, retain) NSArray *thingsArray;
@property (nonatomic, retain) NSArray *fruitsArray;
@property (nonatomic, retain) NSArray *arrays;
@end
TableViewController.m
#import "TableViewController.h"
#import "TableCell.h"
@interface TableViewController ()
@end
@implementation TableViewController
@synthesize tableView;
@synthesize tableViewCell;
@synthesize titlesArray;
@synthesize peopleArray;
@synthesize thingsArray;
@synthesize fruitsArray;
@synthesize arrays;
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
[self.view addSubview:tableView];
titlesArray = [[NSArray alloc] initWithObjects:@"People", @"Things", @"Fruits", nil];
peopleArray = [[NSArray alloc] initWithObjects:@"Gardener.png", @"Plumber.png", @"BusinessWoman.png", @"BusinessMan.png", @"Chef.png", @"Doctor.png", nil];
thingsArray = [[NSArray alloc] initWithObjects:@"StopWatch.png", @"TrashCan.png", @"Key.png", @"Telephone.png", @"ChalkBoard.png", @"Bucket.png", nil];
fruitsArray = [[NSArray alloc] initWithObjects:@"Pineapple.png", @"Orange.png", @"Apple.png", nil];
arrays = [[NSArray alloc] initWithObjects:peopleArray, thingsArray, fruitsArray, nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [arrays count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [titlesArray objectAtIndex:section];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 220;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
TableCell *cell = (TableCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:@"TableViewCell" owner:self options:nil];
CGAffineTransform rotateTable = CGAffineTransformMakeRotation(-M_PI_2);
tableViewCell.tableCellView.transform = rotateTable;
tableViewCell.tableCellView.frame = CGRectMake(0, 0, tableViewCell.tableCellView.frame.size.width, tableViewCell.tableCellView.frame.size.height);
tableViewCell.cellArray = [arrays objectAtIndex:indexPath.section];
tableViewCell.tableCellView.allowsSelection = YES;
cell = tableViewCell;
}
return cell;
}
@end
AppDelegate.m
TableViewController *tvc = [[TableViewController alloc] init];
self.window.rootViewController = tvc;
UITableViewCell by default already has support for images. You do not need to to add a new UIImageView as a subview …
So instead of this:
Write this:
Oh, I now notice that LOTS OF OTHER STUFF is wrong with your code! Sorry to say that. Other stuff you need to change:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathand remove the existing oneI see you’re also doing rotation… but leave that out for now. First try to make it work and THEN worry about rotation of the image.
Can I maybe advise you to read a book on iOS development? It seems as if you don’t know what you’re doing at all 🙁
But I do like to help you out. Can you maybe put all your code on http://www.github.com and invite me as a collaborator? My username is tomvanzummeren. I can make this app work for you.