I am looking to customize an AQGridViewCell by adding a title, date, and image for each cell.
What I have tried so far is:
//View Controller
- (AQGridViewCell *) gridView: (AQGridView *) gridView cellForItemAtIndex: (NSUInteger) index
{
static NSString * CellIdentifier = #"CellIdentifier";
IssueCell * cell = (IssueCell *)[self.gridView dequeueReusableCellWithIdentifier: CellIdentifier];
if ( cell == nil )
{
cell = [[IssueCell alloc] initWithFrame: CGRectMake(0.0, 0.0, 72.0, 72.0) reuseIdentifier: CellIdentifier];
}
//This model object contains the title, picture, and date information
IssueModel *m = (IssueModel *)[self.issues objectAtIndex:index];
[cell setIssueModel:m];
return cell;
}
//Cell class
#import "IssueCell.h"
#import <QuartzCore/QuartzCore.h>
#implementation IssueCell
#synthesize issueModel;
- (id) initWithFrame: (CGRect) frame reuseIdentifier:(NSString *) reuseIdentifier
{
self = [super initWithFrame: frame reuseIdentifier: reuseIdentifier];
if ( self == nil )
return ( nil );
self.contentView.backgroundColor = [UIColor redColor];
self.backgroundColor = [UIColor blueColor];
self.contentView.opaque = NO;
self.opaque = NO;
self.selectionStyle = AQGridViewCellSelectionStyleNone;
return self;
}
#end
My questions is, since init is called before I have access to the model object, where can I setup the title, picture, and date for my cell?
You have to initialize your UI components in the initWithFrame. Example:
In the interface of your IssueCell add UI variables you would like to have:
#interface IssueCell : AQGridViewCell {
UIImageView *im;
UILabel *dateLabel;
}
- (id) initWithFrame: (CGRect) frame reuseIdentifier:(NSString *) reuseIdentifier
{
self = [super initWithFrame: frame reuseIdentifier: reuseIdentifier];
if ( self == nil )
return ( nil );
self.contentView.backgroundColor = [UIColor redColor];
self.backgroundColor = [UIColor blueColor];
self.contentView.opaque = NO;
self.opaque = NO;
self.selectionStyle = AQGridViewCellSelectionStyleNone;
im = [[UIImageView alloc] initWithFrame:yourImageViewFrameHere];
dateLabel = [[UILabel alloc] initWithFrame:yourLabelFrameHere];
[self addSubview:im];
[self addSubview:dateLabel];
return self;
}
#end
Later, you assign desired values in the cellForItemAtIndex method. Example:
- (AQGridViewCell *) gridView: (AQGridView *) gridView cellForItemAtIndex: (NSUInteger) index
{
static NSString * CellIdentifier = #"CellIdentifier";
IssueCell * cell = (IssueCell *)[self.gridView dequeueReusableCellWithIdentifier: CellIdentifier];
if ( cell == nil )
{
cell = [[IssueCell alloc] initWithFrame: CGRectMake(0.0, 0.0, 72.0, 72.0) reuseIdentifier: CellIdentifier];
}
//This model object contains the title, picture, and date information
//
IssueModel *m = (IssueModel *)[self.issues objectAtIndex:index];
[cell.im setImage: m.picture];
[cell.dateLabel setText:[date localizedDescription]];
return cell;
}
Do not store your model data in the UI components. That's a no no. Keep your model separated from the UI. This is only a pseudocode, not tested since I do not have my mac here.
Let me know if it helps.
Related
In my collection view when (custom) cells are reused they, again, get the highlight I have set in didSelectItemAtIndexPath for the original selection. To prevent this, I am using the custom cell's prepareForReuse method, and post calling [super], I check to see if its the selected cell.
If it is I am change the highlight to default else I restore to the original selection highlight when the cell in question is brought back in scroll view's visible area.
Here's the code...
- (void)prepareForReuse
{
[super prepareForReuse];
if (!self.isSelected) {
[self setBackgroundColor:[UIColor systemBackgroundColor]];
[_tagImageView setTintColor:[UIColor systemBlueColor]];
}
else if (self.isSelected)
{
[self setBackgroundColor:[UIColor systemBlueColor]];
[_tagImageView setTintColor:[UIColor systemBackgroundColor]];
}
}
But I notice that the second if block is never executed even when I bring back the original cell in view. This is where I need help. How do I ensure re-highlighting or the original cell/item?
Note, if I try and save the original cell- even though not highlighted, remains the one selected and the corresponding value is saved.
So, this is just about the re-highlight.
Also, here is the selection code...* didSelectItemAtIndexPath*
- (void)collectionView:(UICollectionView *)collectionView
didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
if (selectedIndexPath!=nil) {
if (indexPath.row==selectedIndexPath.row)
{
[tagCollectionView deselectItemAtIndexPath:indexPath animated:YES];
TagCollectionViewCell *selectedCell = (TagCollectionViewCell *)[tagCollectionView cellForItemAtIndexPath:selectedIndexPath];
selectedCell.backgroundColor = [UIColor clearColor];
selectedCell.tagImageView.tintColor = [UIColor systemBlueColor];
selectedIndexPath=nil;
[newDictionary setValue:[NSNull null] forKey:#"type"];
}
else
{
[tagCollectionView deselectItemAtIndexPath:indexPath animated:YES];
TagCollectionViewCell *previousSelectedCell = (TagCollectionViewCell *)[tagCollectionView cellForItemAtIndexPath:selectedIndexPath];
previousSelectedCell.backgroundColor = [UIColor systemBackgroundColor];
previousSelectedCell.tagImageView.tintColor = [UIColor systemBlueColor];
selectedIndexPath = indexPath;
TagCollectionViewCell *selectedCell = (TagCollectionViewCell *)[tagCollectionView cellForItemAtIndexPath:selectedIndexPath];
selectedCell.backgroundColor = [UIColor systemBlueColor];
selectedCell.tagImageView.tintColor = [UIColor systemBackgroundColor];
dictionaryType = _typesArray[selectedIndexPath.row];
[newDictionary setValue:dictionaryType forKey:#"type"];
}
}
else if (selectedIndexPath==nil)
{
selectedIndexPath = indexPath;
TagCollectionViewCell *selectedCell = (TagCollectionViewCell *)[tagCollectionView cellForItemAtIndexPath:selectedIndexPath];
selectedCell.backgroundColor = [UIColor systemBlueColor];
selectedCell.tagImageView.tintColor = [UIColor systemBackgroundColor];
dictionaryType = _typesArray[selectedIndexPath.row];
[newDictionary setValue:dictionaryType forKey:#"type"];
}
}
Any help? Thanks.
Edit:
This is the part of the code that doesn't get called.
else if (self.isSelected)
{
[self setBackgroundColor:[UIColor systemBlueColor]];
[_tagImageView setTintColor:[UIColor systemBackgroundColor]];
}
I think you are way over-complicating things.
A UICollectionView keeps track of its own "selected" cell(s), and calls setSelected on each cell when it is displayed.
You can put all of your "selected" appearance code inside your cell class:
- (void)setSelected:(BOOL)selected {
// change our color properties based on selected BOOL value
self.tagImageView.tintColor = selected ? UIColor.systemBackgroundColor : UIColor.systemBlueColor;
self.backgroundColor = selected ? UIColor.systemBlueColor : UIColor.systemBackgroundColor;
}
Now you don't need to do anything in didSelectItemAt.
Here's a quick example...
SampleCollectionViewCell.h
#interface SampleCollectionViewCell : UICollectionViewCell
- (void)fillData:(NSInteger)n;
#end
SampleCollectionViewCell.m
#import "SampleCollectionViewCell.h"
#interface SampleCollectionViewCell ()
{
UIImageView *theImageView;
UILabel *theLabel;
}
#end
#implementation SampleCollectionViewCell
- (instancetype)init
{
self = [super init];
if (self) {
[self commonInit];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self commonInit];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
[self commonInit];
}
return self;
}
- (void)commonInit {
// add an image view and a label
theImageView = [UIImageView new];
theImageView.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:theImageView];
theLabel = [UILabel new];
theLabel.textAlignment = NSTextAlignmentCenter;
theLabel.font = [UIFont systemFontOfSize:20.0 weight:UIFontWeightBold];
theLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.contentView addSubview:theLabel];
[NSLayoutConstraint activateConstraints:#[
[theImageView.topAnchor constraintEqualToAnchor:self.contentView.topAnchor constant:0.0],
[theImageView.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor constant:0.0],
[theImageView.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor constant:0.0],
[theImageView.bottomAnchor constraintEqualToAnchor:self.contentView.bottomAnchor constant:0.0],
[theLabel.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor constant:0.0],
[theLabel.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor constant:0.0],
[theLabel.bottomAnchor constraintEqualToAnchor:self.contentView.bottomAnchor constant:-4.0],
]];
// image would probably be set by the data source, but
// for this example we'll use the same system image in every cell
UIImage *img = [UIImage systemImageNamed:#"person.fill"];
if (img) {
theImageView.image = img;
}
// let's give the content view rounded corners and a border
self.contentView.layer.cornerRadius = 8.0;
self.contentView.layer.borderWidth = 2.0;
self.contentView.layer.borderColor = UIColor.systemGreenColor.CGColor;
// default (not-selected) colors
theImageView.tintColor = UIColor.cyanColor;
theLabel.textColor = UIColor.blackColor;
self.contentView.backgroundColor = UIColor.systemBackgroundColor;
}
- (void)fillData:(NSInteger)n {
theLabel.text = [NSString stringWithFormat:#"%ld", (long)n];
}
- (void)setSelected:(BOOL)selected {
// change our color properties based on selected BOOL value
theImageView.tintColor = selected ? UIColor.redColor : UIColor.cyanColor;
theLabel.textColor = selected ? UIColor.yellowColor : UIColor.blackColor;
self.contentView.backgroundColor = selected ? UIColor.systemBlueColor : UIColor.systemBackgroundColor;
}
#end
SampleViewController.h
#interface SampleViewController : UIViewController <UICollectionViewDelegate, UICollectionViewDataSource>
#end
SampleViewController.m
#import "SampleViewController.h"
#import "SampleCollectionViewCell.h"
#interface SampleViewController ()
{
UICollectionView *collectionView;
}
#end
#implementation SampleViewController
- (void)viewDidLoad {
[super viewDidLoad];
UICollectionViewFlowLayout *fl = [UICollectionViewFlowLayout new];
fl.scrollDirection = UICollectionViewScrollDirectionVertical;
fl.itemSize = CGSizeMake(60, 60);
fl.minimumLineSpacing = 8;
fl.minimumInteritemSpacing = 8;
collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:fl];
collectionView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:collectionView];
UILayoutGuide *g = [self.view safeAreaLayoutGuide];
[NSLayoutConstraint activateConstraints:#[
// constrain collection view 40-points from all 4 sides
[collectionView.topAnchor constraintEqualToAnchor:g.topAnchor constant:40.0],
[collectionView.leadingAnchor constraintEqualToAnchor:g.leadingAnchor constant:40.0],
[collectionView.trailingAnchor constraintEqualToAnchor:g.trailingAnchor constant:-40.0],
[collectionView.bottomAnchor constraintEqualToAnchor:g.bottomAnchor constant:-40.0],
]];
[collectionView registerClass:SampleCollectionViewCell.class forCellWithReuseIdentifier:#"c"];
collectionView.dataSource = self;
collectionView.delegate = self;
// let's give the collection view a very light gray background
// so we can see its frame
collectionView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return 50;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
SampleCollectionViewCell *c = (SampleCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:#"c" forIndexPath:indexPath];
[c fillData:indexPath.item];
return c;
}
#end
Based on the code you posted, it looks like you want to be able to de-select an already selected cell. If so, add this to the controller:
// this allows us to de-select an already selected cell
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
// get array of already selected index paths
NSArray *a = [collectionView indexPathsForSelectedItems];
// if that array contains indexPath, that means
// it is already selected, so
if ([a containsObject:indexPath]) {
// deselect it
[collectionView deselectItemAtIndexPath:indexPath animated:NO];
return NO;
}
// no indexPaths (cells) were selected
return YES;
}
When run, it starts like this:
Tapping cell "1" selects it:
Tapping cell "7" automatically de-selects cell "1" and selects cell "7":
We can scroll up and down and the selected cell will automatically maintain its "selected appearance":
Edit
To explain why your prepareForReuse wasn't doing what you expected...
The collection view does not set the selected property of the cell until it is going to be displayed.
So, in:
- (void)prepareForReuse
{
[super prepareForReuse];
if (!self.isSelected) {
[self setBackgroundColor:[UIColor systemBackgroundColor]];
[_tagImageView setTintColor:[UIColor systemBlueColor]];
}
else if (self.isSelected)
{
[self setBackgroundColor:[UIColor systemBlueColor]];
[_tagImageView setTintColor:[UIColor systemBackgroundColor]];
}
}
self.isSelected will never be true.
If you want to stick with changing the cell UI properties (colors, tint, etc) in didSelectItemAt, you need to update your cell appearance in cellForItemAt:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
TagCollectionViewCell *c = (TagCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:#"c" forIndexPath:indexPath];
// whatever you are currently doing, such as
//c.tagImageView.image = ...;
if (selectedIndexPath != indexPath) {
[c setBackgroundColor:[UIColor systemBackgroundColor]];
[c.tagImageView setTintColor:[UIColor systemBlueColor]];
}
else
{
[c setBackgroundColor:[UIColor systemBlueColor]];
[c.tagImageView setTintColor:[UIColor systemBackgroundColor]];
}
return c;
}
The program has a tableview. Each TableViewCell has a UICollectionView. When user drag and drop the one of the categories label, the program will add a UICollectionViewCell which has a UIView. It works well. However, when user scroll the tableview then the UICollectionView won't render properly. The display is as below.
This is the pic before user scroll the view.
http://cl.ly/image/1P0s3Z0m443t
After user scroll the view, there is a promise overlap the sin. The promise cell shouldn't be there.
http://cl.ly/image/3L2M1s3s3F0X
Here is my code of cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
// Configure the cell..
/*
There is no UITableViewCell in the storyboard
*/
ContentViewCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UILabel * cellLabel = nil; //The cellLabel is for rendering the bible verses
if (cell==nil) {
cell = [[ContentViewCustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
/*
To Make Dynamic Cell Height
*/
cellLabel = [[UILabel alloc] initWithFrame:CGRectZero];
[cellLabel setNumberOfLines:0];
[cellLabel sizeToFit];
[cellLabel setFont:[UIFont systemFontOfSize:FONT_SIZE]];
[cellLabel setTag:1];
[[cell contentView] addSubview:cellLabel];
}
NSString * text = [verseArray objectAtIndex:[indexPath row]];
CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
CGRect rectangular = [text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:#{NSFontAttributeName: [UIFont systemFontOfSize:FONT_SIZE]} context:nil];
CGSize size = rectangular.size;
/*
When There Are Rows More Than One Screen
*/
if (!cellLabel) {
cellLabel = (UILabel *)[cell viewWithTag:1];
}
cell.CellContent = text;
[cellLabel setText:text];
[cellLabel setFrame:CGRectMake((self.view.frame.size.width*0.39), 0, CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), MAX(size.height, 44.0f))]; //To Locate The Position Of The Label In The Cell
cell.userInteractionEnabled = TRUE;
return cell;
}
And this is ContentViewCustomTableViewCell
#import "ContentViewCustomTableViewCell.h"
#import "AnswerCollectionViewCell.h"
#implementation ContentViewCustomTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
layout.sectionInset = UIEdgeInsetsMake(3, 3, 2, 2);
layout.itemSize = CGSizeMake(30, 10);
layout.scrollDirection = UICollectionViewScrollDirectionVertical;
self.collectionView = [[AFIndexedCollectionView alloc] initWithFrame:CGRectZero
collectionViewLayout:layout];
[self.collectionView registerClass:[AnswerCollectionViewCell class]forCellWithReuseIdentifier:CollectionViewCellIdentifier];
self.collectionView.backgroundColor = [UIColor colorWithRed:0.1 green:0.1 blue:0.1 alpha:0.1];
self.collectionView.showsVerticalScrollIndicator = NO;
[self.contentView addSubview:self.collectionView];
}
return self;
}
- (void)awakeFromNib
{
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(void)layoutSubviews
{
[super layoutSubviews];
self.collectionView.frame = CGRectMake(10, 10, 110, 50);
}
-(void)setCollectionViewDataSourceDelegate:(id<UICollectionViewDataSource, UICollectionViewDelegate>)dataSourceDelegate index:(NSInteger)index
{
self.collectionView.dataSource = dataSourceDelegate;
self.collectionView.delegate = dataSourceDelegate;
self.collectionView.index = index;
[self.collectionView reloadData];
}
#end
I want create one page in way table view. its display 4 cell that different image and name for any cell. (this information get url in loop)
first : I create one class that I can meaning 2 variable.
Recipe.h
#import
#interface Recipe : NSObject
#property (nonatomic, strong) NSString *name; // name of recipe
#property (nonatomic, strong) NSString *imageFile; // image filename of recipe
#end so now in RecipeViewController (root) I write this code :
#import "RecipeViewController.h"
#import "Recipe.h"
#interface RecipeViewController ()
{
IBOutlet UIImageView *ab;
}
#end
#implementation RecipeViewController
{
NSMutableArray *recipes;
NSInteger num;
}
#synthsize ab;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.title = #"Recipe Book";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:nil
action:nil];
[[self navigationItem] setBackBarButtonItem:backButton];
NSString *numberbook = [[NSString alloc]initWithContentsOfURL:[NSURL
URLWithString:#"http://192.168.1.102/mamal/book.php?all"]];
NSInteger numbook = [numberbook integerValue];
NSMutableArray *b = [[NSMutableArray alloc]initWithCapacity:numbook];
for (int i = 1; i <= numbook; i++)
{
Recipe *si = [Recipe new];
NSLog(#"%d,%#",i,si);
NSString *c = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString
stringWithFormat:#"http://192.168.1.102/mamal/book.php?info=1&b=%d",i]]];
NSString *a = [NSString stringWithFormat:#"http://192.168.1.102/mamal/book.php?p=1&b=%d",i];
[b addObject:a];
UIImage *myImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:a]]];
[ab setImage:myImage];
NSLog(#"%#",a);
NSLog(#"%#",c);
si.name = [NSString stringWithString:c];
si.imageFile =[]; //I so confused!!!
if(!recipes){
recipes = [NSMutableArray array];
}
[recipes addObject:si];
}
num = numbook;
// Remove table cell separator
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
// Assign our own backgroud for the view
self.parentViewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"common_bg"]];
self.tableView.backgroundColor = [UIColor clearColor];
// Add padding to the top of the table view
UIEdgeInsets inset = UIEdgeInsetsMake(5, 0, 0, 0);
self.tableView.contentInset = inset;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return recipes.count;
}
- (UIImage *)cellBackgroundForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger rowCount = [self tableView:[self tableView] numberOfRowsInSection:0];
NSInteger rowIndex = indexPath.row;
UIImage *background = nil;
if (rowIndex == 0) {
background = [UIImage imageNamed:#"cell_top.png"];
} else if (rowIndex == rowCount - 1) {
background = [UIImage imageNamed:#"cell_bottom.png"];
} else {
background = [UIImage imageNamed:#"cell_middle.png"];
}
return background;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
}
// Display recipe in the table cell
Recipe *recipe = [recipes objectAtIndex:indexPath.row];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [UIImage imageNamed:recipe.imageFile];
UILabel *recipeNameLabel = (UILabel *)[cell viewWithTag:101];
recipeNameLabel.text = recipe.name;
UILabel *recipeDetailLabel = (UILabel *)[cell viewWithTag:102];
recipeDetailLabel.text = recipe.detail;
// Assign our own background image for the cell
UIImage *background = [self cellBackgroundForRowAtIndexPath:indexPath];
UIImageView *cellBackgroundView = [[UIImageView alloc] initWithImage:background];
cellBackgroundView.image = background;
cell.backgroundView = cellBackgroundView;
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
#end
I so confused and get this error :
"connection cannot have a prototype object as its destination"
also I don't know one way for display image in cell!!!
cell.imageView.image = yourImage
Try this
cell.yourImage.image = [UIImage imageNamed:#"imageName"];
I know the question has already been asked and answered but I cant find the solution.
I've got this error even if I'm not pushing the incriminated view controller :
Unbalanced calls to begin/end appearance transitions for .
here is my code NewsViewController.m
#import "NewsViewController.h"
#import "XMLToObjectParser.h"
#import "UneNews.h"
#define USE_CUSTOM_DRAWING 1
#interface NewsViewController ()
#end
#implementation NewsViewController
#synthesize imageView;
#synthesize vueDetail;
#synthesize tableauNews,tableViewFluxRSS;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil tableauDeNews:(NSMutableArray *)tableauDeNews
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
//récupération du tableau de news initialisé
tableauNews = tableauDeNews;
}
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
tableViewFluxRSS.separatorStyle = UITableViewCellSeparatorStyleNone;
tableViewFluxRSS.rowHeight = 143;
tableViewFluxRSS.backgroundColor = [UIColor clearColor];
}
- (void)viewDidUnload
{
[self setImageView:nil];
[self setVueDetail:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return NO;
}
//Navigation
- (IBAction)goHome:(id)sender{
[self.navigationController popToRootViewControllerAnimated:YES];
}
// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Navigation logic may go here -- for example, create and push another view controller.
[vueDetail loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[[tableauNews objectAtIndex:indexPath.row] detail]]]];
}
//gestion du UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [tableauNews count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
const NSInteger TOP_LABEL_TAG = 1001;
const NSInteger BOTTOM_LABEL_TAG = 1002;
const NSInteger DATE_LABEL_TAG = 1003;
const NSInteger ANNEE_LABEL_TAG = 1004;
UILabel *topLabel;
UILabel *bottomLabel;
UILabel *dateLabel;
UILabel *anneeLabel;
UILabel *enSavoirPlus;
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableViewFluxRSS dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
//
// Create the cell.
//
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier];
//
// Configure the properties for the text that are the same on every row
//
dateLabel = [[UILabel alloc]
initWithFrame:
CGRectMake(6,46,75,57)];
dateLabel.tag = DATE_LABEL_TAG;
dateLabel.backgroundColor = [UIColor clearColor];
dateLabel.textColor = [UIColor whiteColor];
dateLabel.font = [UIFont systemFontOfSize:28];
[cell.contentView addSubview:dateLabel];
//
// Configure the properties for the text that are the same on every row
//
anneeLabel = [[UILabel alloc]
initWithFrame:
CGRectMake(6,100,70,57)];
anneeLabel.tag = DATE_LABEL_TAG;
anneeLabel.backgroundColor = [UIColor clearColor];
anneeLabel.textColor = [UIColor whiteColor];
anneeLabel.font = [UIFont systemFontOfSize:31];
[cell.contentView addSubview:anneeLabel];
topLabel =
[[UILabel alloc]
initWithFrame:
CGRectMake(94,5,325,20)];
[cell.contentView addSubview:topLabel];
//
// Configure the properties for the text that are the same on every row
//
topLabel.tag = TOP_LABEL_TAG;
topLabel.backgroundColor = [UIColor clearColor];
topLabel.textColor = [UIColor whiteColor];
topLabel.font = [UIFont systemFontOfSize:18];
//
// Configure the properties for the text that are the same on every row
//
bottomLabel =
[[UILabel alloc]
initWithFrame:
CGRectMake(94,30,325,80)];
bottomLabel.tag = BOTTOM_LABEL_TAG;
bottomLabel.backgroundColor = [UIColor clearColor];
bottomLabel.textColor = [UIColor whiteColor];
bottomLabel.font = [UIFont systemFontOfSize:18];
[bottomLabel setLineBreakMode:UILineBreakModeWordWrap];
[bottomLabel setNumberOfLines:0];
[cell.contentView addSubview:bottomLabel];
//
// Create a background image view.
//
cell.backgroundView =
[[UIImageView alloc] init];
cell.selectedBackgroundView =
[[UIImageView alloc] init];
enSavoirPlus =
[[UILabel alloc]
initWithFrame:
CGRectMake(260,121,200,20)];
[cell.contentView addSubview:enSavoirPlus];
//
// Configure the properties for the text that are the same on every row
//
enSavoirPlus.tag = TOP_LABEL_TAG;
enSavoirPlus.backgroundColor = [UIColor clearColor];
enSavoirPlus.textColor = [UIColor yellowColor];
//topLabel.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
enSavoirPlus.font = [UIFont systemFontOfSize:18];
}
else
{
topLabel = (UILabel *)[cell viewWithTag:TOP_LABEL_TAG];
bottomLabel = (UILabel *)[cell viewWithTag:BOTTOM_LABEL_TAG];
dateLabel = (UILabel *)[cell viewWithTag:DATE_LABEL_TAG];
anneeLabel = (UILabel *)[cell viewWithTag:ANNEE_LABEL_TAG];
}
topLabel.text = [[tableauNews objectAtIndex:indexPath.row] titre];
bottomLabel.text = [[tableauNews objectAtIndex:indexPath.row] contenu];
[bottomLabel sizeToFit];
dateLabel.text = [[tableauNews objectAtIndex:indexPath.row] dateDeParution];
anneeLabel.text = [[tableauNews objectAtIndex:indexPath.row] annee];
enSavoirPlus.text = #"En savoir plus...";
//
// Set the background and selected background images for the text.
// Since we will round the corners at the top and bottom of sections, we
// need to conditionally choose the images based on the row index and the
// number of rows in the section.
//
UIImage *rowBackground;
rowBackground = [UIImage imageNamed:#"fd-textes-news.png"];
//selectionBackground = [UIImage imageNamed:#"middleRowSelected.png"];
((UIImageView *)cell.backgroundView).image = rowBackground;
//((UIImageView *)cell.selectedBackgroundView).image = selectionBackground;
return cell;
}
//fin gestion UITableView
#end
I think my problem may be due to my implementation of the init
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil tableauDeNews:(NSMutableArray *)tableauDeNews;
need help
thx
I found the problem:
To create my buttons I copy paste them in the interfaceBuilder,
the copied buttons have kept their old actions, and I had a new IBAction.
My copied buttons had 2 IBactions (one calling a NewsViewController and another calling the right viewController).
The message was poping each time I clicked the copied button.
So check your buttons in the interface Builder, "Unbalanced calls to begin/end appearance transitions" can be caused by buttons having two IBActions, conflictual (eg. pushing two different viewControllers at the same time).
I have a custom UITableViewCell class and would like to display images and strings linearly. For example:
Row 1: [Image1] string1 [Image2] string2 [Image3]
Row 2: [Image4] string3 [Image5]
The images have varying widths but I would like equal spacing. How would I do this? I have tried manipulating subviews and CGRectMake to no avail.
Additionally, I am using an NSDictionary to hold the content and the number of images/string is not constant for each cell.
My CustomCell class:
#import "CustomCell.h"
#implementation CustomCell
#synthesize primaryLabel,secondaryLabel,image1;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
// Initialization code
primaryLabel = [[UILabel alloc]init];
primaryLabel.textAlignment = UITextAlignmentLeft;
primaryLabel.font = [UIFont systemFontOfSize:16];
secondaryLabel = [[UILabel alloc]init];
secondaryLabel.textAlignment = UITextAlignmentLeft;
secondaryLabel.font = [UIFont systemFontOfSize:14];
image1 = [[UIImageView alloc]init];
[self.contentView addSubview:primaryLabel];
[self.contentView addSubview:secondaryLabel];
[self.contentView addSubview:image1];
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
CGRect frame;
frame= CGRectMake(0 ,5, 200, 25);
primaryLabel.frame = frame;
frame= CGRectMake(0 ,30, 200, 25);
secondaryLabel.frame = frame;
frame= CGRectMake(0, 60, 23, 20);
image1.frame = frame;
...
My RootViewController
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
// Set up the cell...
NSDictionary *dictionary = nil;
//Search
if (tableView == self.searchDisplayController.searchResultsTableView)
{
dictionary = [self.filteredListContent objectAtIndex:indexPath.row];
}
else
{
dictionary = [self.tableDataSource objectAtIndex:indexPath.row];
}
//Original
cell.primaryLabel.text = [dictionary objectForKey:#"Title"];
for (NSArray *keystroke in [dictionary objectForKey:#"Strokes"]) {
for (int i = 0; i < 2; i++) {
if ([(NSString *)keystroke isEqualToString:#"string1"] || [(NSString *)keystroke isEqualToString:#"string2"]) {
cell.secondaryLabel.text = (NSString *)keystroke;
}
else {
NSString *imageFilePath = [NSString stringWithFormat:#"%#.png", keystroke];
NSLog(#"%#", imageFilePath);
UIImage *myimage = [UIImage imageNamed:imageFilePath];
cell.image1.image = myimage;
}
}
}
return cell;
}
...
Obviously there are a lot of holes here. Primarily, as I loop through my dictionary, I need to move my CustomCell subview right so I can place the images/text next to the previous subview.
You are on the right track. In your UITableViewCell subclass, you will need to override layoutSubviews, define CGRects for each UIImageView or UILabel manually, and set them as the respective view's frame.
Check out CGRectGetMaxX. It will be very useful in this context.