using UIImageView in accessoryView in UITableViewCell - iphone

I am trying to use a custom image for the UIAccessoryView, however I can't seem to get it to work. When the table view launches, it doesn't update with any of the images. My code is the following:
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *tableViewCell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
MenuItem *menuItem = [self.menuItems objectAtIndex:indexPath.row];
if (!tableViewCell)
{
tableViewCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
tableViewCell.textLabel.font = [UIFont fontWithName: CaptionDistractionBodyFontName size: 15];
imageView.contentMode = UIViewContentModeCenter;
tableViewCell.accessoryView = [[UIImageView alloc] initWithImage:nil];
}
tableViewCell.textLabel.text = menuItem.name;
UIImageView *imageView = (UIImageView*)tableViewCell.accessoryView;
imageView.image = nil;
if (menuItem.type == MenuItemTypeCheckMark)
{
if (menuItem.isCheckMarked)
{
imageView.image = [UIImage imageNamed:#"DisclosureViewX"];
}
}
else if (menuItem.type == MenuItemTypeSubmenu)
{
imageView.image = [UIImage imageNamed:#"DisclosureViewArrow"];
}
return tableViewCell;
}
I've tried a lot of different stuff, i.e. calling setNeedsLayout, moving the code to a custom UITableViewCell in layoutSubviews and nothing seems to work. Aside from just creating a whole new accessory view myself, what is the correct way to go about this?

Since you initially setup the UIImageView with a nil image, the image view's frame has a zero width and height. You need to reset the frame after assigning an actual image.
imageView.image = [UIImage imageNamed:#"DisclosureViewX"];
imageView.frame = CGRectMake(0, 0, imageView.image.size.width, imageView.image.size.height);
Edit:
Actually it would be even easier to call [imageView sizeToFit] after setting the image instead of setting the frame.

try doing
tableViewCell.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"DisclosureViewX"]];

I think your problem is that image view creates with zero size. Use initWithFrame: or initWithImage:, where image is not nil. After created with initWithImage:, image view will have bounds set to image size. A cell will center the accessory view automatically.

Related

Custom Separator Image disappears in UItableviewcell

I am loading custom separator image in uitableview cell.
Here is my code :
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellID=#"Cell"
MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SwitchCellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIImageview *aSwitch = [[UIImageview alloc] initWithImage:[UIImage imageNamed:#"divider.png"]];
separator.frame = CGRectMake(0,50,320,1);
[cell.contentView addSubview:seperator];
}
if(cell.height == 22)
{
/// here i am setting frame for uiimageview
}
but i am getting seperator image disappears for only one row out of 20 while scrolling.
Can you please help why it is loading like this.
If you've put your separator UIImage out of the bounds of the cell, and set cell.clipsToBounds = NO; to have the separator image displayed, the separator image might get hidden by drawing the next cell.
You can't control the z-index of the cells as they're being drawn on screen, it depends from where you're scrolling (bottom to top, or top to bottom).
If that's indeed you're issue, you can either put the divider inside the cell's frame, or if your separator is thin enough use:
[TableView setSeparatorColor:[UIColor colorWithPatternImage:[UIImage imageNamed:...]]];
self.tblView=[[UITableView alloc] initWithFrame:CGRectMake(0,0,320,370) //set as u need
style:UITableViewStylePlain];
self.tblView.delegate=self;
self.tblView.dataSource=self;
[self.view addSubview:self.tblView];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 10)];//set as u ne
v.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"divider.png"]];
[self.tblView setTableHeaderView:v];
[self.tblView setTableFooterView:v];
[v release];

How to set a background image in a static UITableViewCell?

I tried the following code, but the cell doesn't change at all:
- (void)viewDidLoad
{
[super viewDidLoad];
//Set background image of cells
UITableViewCell *cell;
cell = [CalculatorTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Spots.png"]];
}
Two things. First of all, cells should be created in the cellForRowAtIndexPath method. Secondly, you should make your background view and then add it to the cell content view.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
cell = [CalculatorTableView alloc] initWithStyle:UITableViewStyleDefault reuseIdentifier:nil]; //This wil obviously depend on your subclass
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Spots.png"]];
[cell.contentView addSubview:backgroundView];
}
cell.backgroundView = [ [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:#"Spots.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]autorelease];
That might be a better way to do it.
OR, you can try this:
You can try this code that will help you.
Put this code in cellForRowAtIndexPath method
UIImageView *av = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 277, 58)];
av.backgroundColor = [UIColor clearColor];
av.opaque = NO;
av.image = [UIImage imageNamed:#"Spots.png"];
cell.backgroundView = av;
You put your cell initializing code in viewDidLoad? That is very wrong, it should be in:
cellForRowAtIndexPath:
You might want to check out this link:
TableView programming
Cells created using static table views don't exist until after viewWillAppear: has been called. Move your code above into that method (and make sure you call the super implementation first.

How do I horizontally center a UIImageView in a UITableViewCell?

I am having trouble centering a 200x200 image horizontally inside a customized UITableViewCell. I have set the tableViewheightForRowAtIndexPath in the UITableViewDelegate to the appropriate value, but that doesn't center my image horizontally, it does so only vertically (better than nothing I guess). I also tried to change the value of the imageView.frame in layoutSubviews in my customized cell, but that doesn't seem to have any effect. I have even commented it out, placed it before [super layoutSubviews];, played around with the values of the frame, but I keep getting the same result.
Can anyone help me with this? What code should I write to achieve such an effect?
Thanks.
You will try this code its help you
You need to subclass UITableViewCell and override layoutSubviews, as follows:
- (void) layoutSubviews
{
[super layoutSubviews];
self.imageView.frame = CGRectMake( 10, 10, 50, 50 ); // your positioning here
}
In your cellForRowAtIndexPath: method, be sure to return an instance of your new cell type.
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
UIImageView *imgView;
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
imgView = [[UIImageView alloc] initWithFrame:(100,0,100,62)];
[imgView setImage:[UIImage imageNamed:#"img.png"]];
imgView.tag = 55;
[cell.contentView addSubview:imgView];
[imgView release];
}
else
{
imgView = (id)[cell.contentView viewWithTag:55];
}

Transparency between UITableViewCells

I want to have transparency between UITableViewCells. Space between the cells.
I user custom created cells and for setting a background i use this code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CustomCell = #"CustomBookingCell";
currentBooking = [arrayBookings objectAtIndex:indexPath.row];
CustomBookingCell *cell = (CustomBookingCell *)[tableView dequeueReusableCellWithIdentifier:CustomCell];
if (cell == nil) {
UIViewController *c = [[UIViewController alloc] initWithNibName:#"CustomBookingCell" bundle:nil];
cell = (CustomBookingCell *)c.view;
[ c release ];
}
bookingImage = [[UIImageView alloc] initWithImage:
[UIImage imageNamed:currentBooking.imageSource]];
[cell.imageForBooking addSubview:bookingImage];
cell.selectionStyle = UITableViewCellSelectionStyleGray;
cell.contentView.backgroundColor = [UIColor clearColor];
UIView* backgroundView = [[UIView alloc] initWithFrame:CGRectZero];
UIImage* bgImage = [UIImage imageNamed:#"Background_300_82.png"];
UIColor *bgColor = [[UIColor alloc] initWithPatternImage: bgImage];
backgroundView.backgroundColor = bgColor;
cell.backgroundView = backgroundView;
cell.label.text = currentBooking.title;
[bookingImage release];
[bgColor release];
[backgroundView release];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 90;
}
The height of the cell is 10 pixels higher that the tableCellBg.png.
My background view has also a image as background (this background is supposed to be shown between the cells of course).
So I tried to add 10 pixels with transparency to my tableCellBg.png in the bottom to fix this. But the space between the cells is black. I can't see the view background between my cells.
What shall I do? Do I have to create a UIView in cellForRowAtIndexPath with the height of the tableCellBg.png and then add the Custom UITableViewCell as subview to the created UIView with a less higher height?
Or is there a much more simplyfied way to accomplish this?
Your table view needs a clear background colour. For example
myTableView.backgroundColor = [UIColor clearColor];
I solved it by using another method to add the background image to the UITableViewCell:
UIImage *image = [UIImage imageNamed:#"ny_bg_event.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(0, 0, 300, 84);
cell.backgroundView = imageView;
cell.backgroundColor = [UIColor clearColor];
[imageView release];
[cell setFrame:CGRectMake(0, 0, 300, 84)];
cell.contentView.backgroundColor = [UIColor clearColor];
Instead of creating a UIView I just used a UIImageView instead.

UITableView is loading every cell

UITableview should only load cells that is visible at first right? My tableview is loading every cell initially which slows it down a lot. I'm using around 1000 rows. Only want it to load a cell when it has to (like user scrolling down). Anyone have any ideas why it's doing this?
I know cellForRowAtIndexPath is getting called initially for every cell. The height of the cells is 89.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UILabel *textName = nil;
UIImageView* image = nil;
unsigned int DATA_TAG = 1001;
unsigned int IMG_TAG = 1002;
// Retrieve a cell is Available
cell = [aTableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Check if no new cell was available
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
// Set the Accessory Data
textName = [[[UILabel alloc]initWithFrame:CGRectMake(cell.frame.origin.x, 80, cell.frame.size.width, 20)]autorelease];
textName.tag = DATA_TAG;
textName.textAlignment = UITextAlignmentCenter;
textName.backgroundColor = [UIColor clearColor];
textName.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
textName.textColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
textName.lineBreakMode = UILineBreakModeWordWrap;
[cell.contentView addSubview:textName];
//Set the Image Data
image = [[[UIImageView alloc]initWithFrame:CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, 80)]autorelease];
image.tag = IMG_TAG;
image.contentMode= UIViewContentModeScaleAspectFit;
[cell.contentView addSubview:image];
}
Accessory* acc= [[AccessoryManager sharedManager].currentList objectAtIndex:indexPath.row];
if(acc == nil)
return cell;
textName= (UILabel*)[cell.contentView viewWithTag:DATA_TAG];
textName.text= acc.accessoryName;
image= (UIImageView*)[cell.contentView viewWithTag:IMG_TAG];
[image setImage:acc.accessoryImage];
return cell;
}
Can you please post some code? The method -tableView:cellForRowAtIndexPath is only called when a new "slot" for a potential cell opens up, so you'd need to be doing something majorly wrong to be "loading every cell" initially!
Do you perhaps mean that you're loading all the data initially, and wish to do that in batches?
That's right - it should only load the ones it has to. What is your UITableView's rowHeight? If that was extremely low, the Table might expect to have to load all the cells
If that's not the problem, can you paste in your tableView:cellForRowAtIndexPath: code?
Are you invoking cellForRowAtIndexPath yourself, for example from heightForRowAtIndexPath? If so, you don't need to create the cell to determine its height.