Custom Separator Image disappears in UItableviewcell - iphone

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];

Related

content in tableview cell overlapping

I created a prototype cell in storyboard with four labels
then in my code i am adding different background images for first,last and other rows.
for first row all 4 labels are hidden.
Intially all 5 rows shown in screen appear good.but when i scroll up to see 6th row ,it behaves like first row and all 4 labels are hidden.
here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Employee *currentEmployee=[Employee sharedData];
NSArray *tempArray =currentEmployee.leaveApproveDict;
static NSString *simpleTableIdentifier = #"approveLeaveCell";
approveLeaveCell *cell = [self.approveLeaveView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[approveLeaveCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
if([tempArray count]!=0){
if(indexPath.row == 0){
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"h_t_top_bar.png"]];
cell.backgroundView = imgView;
cell.fromLabel.hidden=YES;
cell.toLabel.hidden=YES;
cell.typeLabel.hidden=YES;
cell.nameLabel.hidden=YES;
[cell setUserInteractionEnabled:NO];
}
else{
if(indexPath.row ==[tempArray count]){
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"h_t_bottom_bar.png"]];
cell.backgroundView = imgView;
}
else{
UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"h_t_repeat_bar.png"]];
cell.backgroundView = imgView;
}
//assign the data
if(indexPath.row-1 < [tempArray count]){
NSDictionary *tempLeave= [currentEmployee.leaveApproveDict objectAtIndex:indexPath.row-1];
cell.toDateLabel.text=[self extractLeaveDate:(NSString*) [tempLeave objectForKey:#"leaveTo"]];
cell.fromDateLabel.text=[self extractLeaveDate:(NSString*)[tempLeave objectForKey:#"leaveFrom"]];
cell.leaveLabel.text=[tempLeave objectForKey:#"leaveType"];
cell.employeeNameLabel.text=[tempLeave objectForKey:#"requesterName"];
}
}
return cell;
}
The same issue happens with me. I have solved it using two different customized UITableViewCell and two identifiers.
A UITableView object maintains a queue (or list) of the currently reusable cells, each with its own reuse identifier, and makes them available to the delegate in the dequeueReusableCellWithIdentifier: method.
Did you use ARC in your project? If you did not use arc, then cell should use autorelease:
cell = [[[approveLeaveCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier] autorelease];
/* Remove all existing content from list and reload them again to avoid
* overlay of cell content */
for(UIView *view in cell.contentView.subviews){
if ([view isKindOfClass:[UIView class]]) {
[view removeFromSuperview];
}
}
// VKJ

Custom separator image used in UITableView disappears when scrolling the UITableView

I am using a simple UITableView and using separator as a image. When table is loaded initially it looks fine but when user scrolls up and leaves, the separator image disappears. Again when user scrolls down and leaves, lines appears again.
Please let me know how to resolve this.
- (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];
}
..........
It's not clear where you declare "separator" but you'll want to make sure you initialize and allocate it as a separate instance for each cell.
UIView *separator = [[UIView alloc] initWithFrame:CGRectMake(0, 50, 320, 1)];
[cell.contentView addSubView:separator];
Are you trying to use one separator instance for all separators in the table? That won't work because a view can only be added to a single superview. You should initialize a new separator for each cell in your reuse pool.

image in an Expanded TableViewCell is lost after scrolling

I am creating an application in which i have many table cells(>300). I have successfully implemented expanding and collapsing tableview cells. On expansion an image is displayed which is fetched from a webservice, on clicking the expanded cell, the cell collapses again, if another cell is clicked the previous cell collapsed and current cell expands. Now the problem is if the user scrolls the tableview with the cell expanded table works fine but when he returns
to it the image gets lost, and there is a bit of problem with the functionality as well( the table takes that cell to be collapsed).
Moreover if the user keeps on scrolling the table down he can encounter that image in a collapased cell(which looks very bad), i think its because dequeueReusableCellWithIdentifier is fetching the already expanded cell.
PS: Every image is different based on the cell value but it is fetched in a different call(not al together ie to fetch 300 images i make 300 independent calls).
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UIImageView * imageView;
UILabel * nameLabel;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(45.0, 5.0, 245.0, 34.0)];
nameLabel.font = [UIFont fontWithName:CELL_FONT size:CELL_FONT_SIZE];
nameLabel.tag = 2;
nameLabel.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:nameLabel];
[nameLabel release];
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(5.0, 5.0, 30.0, 30.0)];
imageView.tag = 1;
imageView.contentMode = UIViewContentModeScaleAspectFit;
[cell.contentView addSubview:imageView];
[imageView release];
}
else {
imageView = (id)[cell.contentView viewWithTag:1];
nameLabel = (id)[cell.contentView viewWithTag:2];
}
return cell;
}

Custom UITableViewCell background goes transparent after selection

When selecting, and holding, one of my custom cells for a UITableView plain-style table, it displays my custom selection color (redColor in this case), but then if I keep it held down and scroll, the custom selection color and the custom background disappear (showing through to the UIImageView behind the table.
The UITableView is created in IB and has a background set to clearColor to allow an image to show through.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:kCellIdentifier]
autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
/* Background */
cell.backgroundView = [[[UIImageView alloc] init] autorelease];
cell.selectedBackgroundView = [[[UIImageView alloc] init] autorelease];
}
cell.backgroundView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:#"customBackground"]];
cell.selectedBackgroundView.backgroundColor = [UIColor redColor];
return cell;
}
Can anyone help? Thanks!
Ok I solved this. Basically you cannot use initWithPatternImage when customizing a UITableViewCell. After it is selected, it's background view must either go transparent, or be removed from the view hierarchy.
Use the following:
((UIImageView*)cell.backgroundView).image = [UIImage imageNamed:#"customBackground"];
((UIImageView*)cell.selectedBackgroundView).image = [UIImage imageNamed:#"selectedBackground"];

Centering Subviews in UITableViewCell within grouped TableView

I'm trying to figure out how to place a subview to a custom UITableViewCell with centered position. As stated in the documentation (Customizing Cells) one should "...add subviews to the contentView property of the cell object or ...". As I noticed it works fine with UITableViewStylePlain. In UITableViewStyleGrouped tables, subviews can not be placed centered. They are shifted to the right hand side.
If the subview is added to UITableViewCell directly, it's fine with both modes. The same is true, if I subclass the UITableViewCell and load the cell from XIB.
Unfortunately, I can't upload screenshots. Can anyone help fix the issue? Or do I miss something vital, as the misalignment seems to match the dimension to be shrunken in grouped table.
Many thanks,
El
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier1 = #"AddToCell";
static NSString *cellIdentifier2 = #"AddToContentView";
static NSString *aVeryLongLine = #"---------------------------------------------";
UITableViewCell *cell = nil;
if (indexPath.section == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier1] autorelease];
CGRect frame = cell.frame;
CGRect labelFrame = CGRectMake((frame.size.width-270)/2, (frame.size.height-40)/2, 270, 40);
UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
label.textAlignment = UITextAlignmentCenter;
label.text = aVeryLongLine;
[cell addSubview:label];
[label release];
}
}
else if (indexPath.section == 1) {
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier2];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier2] autorelease];
CGRect frame = cell.contentView.frame;
CGRect labelFrame = CGRectMake((frame.size.width-270)/2, (frame.size.height-40)/2, 270, 40);
UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
label.textAlignment = UITextAlignmentCenter;
label.text = aVeryLongLine;
[cell.contentView addSubview:label];
[label release];
}
}
else {
}
return cell;
}
Instances of UITableViewCell resize their content views under a number of circumstances (for example, when temporarily adding a delete button to allow the user to remove a row).
When cells are displayed in a grouped style table view, they're resized to provide a margin between the cells and the left and right edges of the table view. So for your subviews to stay positioned correctly, you'll need to set their autoresizing masks appropriately, something like this:
mySubview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
(or
[mySubview setAutoresizingMask:(UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin)];
for folks who prefer the old syntax.)
Did you check the frame's value, which come from cell.contentView.frame or cell.frame ?
When the cell is initialized with style and identity, it maybe have a CGZeroRect for his frame value.