Improper functioning tableView data and recharging - ios

-Hi, I have a problem with a tableview reloading data and what I want to do is that you put the selected cell in blue and change an image that is, the problem is that if I make the [self.mytableview reloadData] in the didselectedRow blue background disappears and if I do the image of the cell does not change, I'm a bit lost with this piece of code I give thanks
if (indexPath.row == _selectedRow) {
UIImageView *favView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"favIconSelected.png"]];
CGRect frame = favView.frame;
frame.origin.x = 294;
frame.origin.y = 7;
favView.frame = frame;
[cell.contentView addSubview:favView];
[favView release];
}
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:SELCETED_BGIMGCELL]]autorelease];
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Product *p = [_productList objectAtIndex:indexPath.row];
_selectedRow=indexPath.row;
[_delegate productWasSelected:p];
[self.myTableView reloadData];
}

You don't need to call reloadData at all. All you need to do is to update the cell that was selected.
In the didSelectRowAtIndexPath method you can get the cell directly by calling cellForRowAtIndexPath on the tableView. This will return the actual cell that's on display and you can directly set the image it's displaying.
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.imageView.image = ...;
Alternatively you could control the image display from your cell subclass if you provide it with both the normal and selected images when you configure each instance.

Related

UIImage not shown as expected

- (void)showImageAtTableView:(UITableView *)tableView forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([tableView indexPathsForSelectedRows].count) {
if ([[tableView indexPathsForSelectedRows] indexOfObject:indexPath] != NSNotFound) {
//cell is expanded
CGFloat frameHeight = [[self.widthArray objectAtIndex:indexPath.row] floatValue];
CGFloat frameWidth = [[self.heightArray objectAtIndex:indexPath.row] floatValue];
//CGFloat yOrigin = [self configureHeightForRowAtIndexPath:indexPath];
CGRect imageFrame = CGRectMake(0,
12,
frameWidth,
frameHeight);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageFrame];
imageView.image = [UIImage imageNamed:#"testImage.jpg"];
imageView.contentMode = UIViewContentModeCenter;
//configue which cell
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
[cell.contentView addSubview:imageView];
}
}
}
the code above is supposed to add a UIImageView to an existing UITableViewCell when tapped. this is the line that call that method (showImageAtTableView:forRowAtIndexPath) :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath: (NSIndexPath *)indexPath
{
[self updateTableView];
//get and show the image at selected cell
[self showImageAtTableView:tableView forRowAtIndexPath:indexPath];
}
but this is the result when the tableviewcell is tapped:
http://imgur.com/4NMJLf6,XweOgQu
what did I do wrong?
You're not changing the height of the UITableViewCell.
I think a better approach would be to include the UIImageView in the xib / storyboard design. Assuming you have some kind of dto object storing the data showed in the cells, you can keep on them a boolean indicating whether or not the image should be showed.
On your didSelectRowAtIndexPath you'll update that boolean, and do a [tableView reloadData].
In your cellForRowAtIndexPath you'll hide / show the UIImageView according the boolean.
In your heightForRowAtIndexPath you'll check again the boolean value in order to update the height accordingly.
Hope it helps.

UIScrollView in UITableView content problems

I have a scrollview inside each cell in my tableview, although the pictures embed in my scrollview don't show up until i scroll the tableview up and down...
I have a custom class with a #property for the scrollview and the cell. I have my code for setting the scrollview in the cellForRowAtIndexPath: method. This should be called upon initial creation of the cells as well? Im confused.
How can I get rid of the problem and make the images appear at first when I start the app?
Related code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = #"CustomID";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
for (int i = 0; i < [imageArray count]; i++) {
CGRect frame;
frame.origin.x = cell.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = cell.scrollView.frame.size;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
imageView.image = [UIImage imageNamed:[imageArray objectAtIndex:i]];
[cell.scrollView addSubview:imageView];
}
cell.scrollView.contentSize = CGSizeMake(cell.scrollView.frame.size.width * [imageArray count], cell.scrollView.frame.size.height);
return cell;
}
This is not exactly related to your question but you will run into this issue too:
Don't forget that your cells will be reused. Upon reusage (lets say the user scrolls down, a cell is moved upwards off the screen and the next that appears on the bottom will be physically the instance of CustomCell that just disappeard and was reused.)
Therefore add:
for (UIView *aView in [NSArray arrayWithArray:cell.subviews]) {
[aView removeFromSuperview];
// if you don't arc then release them and take care of their subviews - if any.
}
before adding any new UIImageView.
(I thought there was a method for removing all subviews in one go but did not find it)

UIImageView added to cell contentView displaying randomly when scrolling through UITableView

My UITableView is pulling data from an array. Items in this array have a property called IsSelected. I am trying to put a UIImageView in the cell contentView for each item that is selected.
The UITableView however when reusing the cell is causing my image to be reused on cells that it shouldn't. I cannot figure out for the life of me how I should be doing it differently. I have attached a screen shot showing the issue. If I keep scrolling up and down the image goes all over the place:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SchoolCellIdentifer = #"SchoolCellIdentifier";
SchoolInfoItem *item = [self.schoolsArray objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SchoolCellIdentifer];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SchoolCellIdentifer];
cell.contentView.backgroundColor = [BVColors WebDarkBlue];
}
cell.textLabel.text = item.Name;
if ([item.selected isEqualToString:#"1"])
{
cell.contentView.backgroundColor = [BVColors WebBlue];
UIImageView *selectedItemCheckMarkIcon = [[UIImageView alloc] initWithFrame:CGRectMake(300, 13, 17, 17.5)];
[selectedItemCheckMarkIcon setImage:[UIImage imageNamed:#"check-mark.png"]];
[cell.contentView addSubview:selectedItemCheckMarkIcon];
}
else
{
cell.contentView.backgroundColor = [BVColors WebDarkBlue];
}
return cell;
}
You need to make sure the UIImageView is getting removed from the cell content view. It looks like in your code when a cell gets dequeued that imageview is still in the cells view hierarchy.
Best solution is to have your cell hold onto a reference to the image view and remove it when necessary.
Take the following:
if ([item.selected isEqualToString:#"1"])
{
cell.contentView.backgroundColor = [BVColors WebBlue];
cell.myImageView = [[UIImageView alloc] initWithFrame:CGRectMake(300, 13, 17, 17.5)];
[selectedItemCheckMarkIcon setImage:[UIImage imageNamed:#"check-mark.png"]];
[cell.contentView addSubview:cell.myImageView];
}
else
{
[cell.myImageView removeFromSuperview];
cell.myImageView = nil;
cell.contentView.backgroundColor = [BVColors WebDarkBlue];
}
Note in the else case the removal of the imageview.
You keep adding a the UIImageView as a subview on the cell's contentView. This isn't removed when the table view is reusing the cell. You'll need to remove the subview if it shouldn't appear.
You should make selectedItemCheckMarkIcon a property on your UITableViewCell subclass. And then have a method in your subclass where you set the image or visibility of the image accordingly.
You could also use the accessoryView property on the UITableView and set the imageView as that:
if ([item.selected isEqualToString:#"1"]) {
cell.contentView.backgroundColor = [BVColors WebBlue];
UIImageView *selectedItemCheckMarkIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"check-mark.png"]];
cell.accessoryView = selectedItemCheckMarkIcon;
} else {
cell.accessoryView = nil;
cell.contentView.backgroundColor = [BVColors WebDarkBlue];
}
Note that you don't need to set a frame in this case because the system will automatically set the frame correctly for the accessoryView.

IOS: Custom UITableViewCell - Strange Behaviour after Scrolling

i have a TableView with custom table cells. I add programmatically borders at the bottom of each cell to keep the screendesign layout. Everything is fine, when the App loads the first time. But after scrolling (and scrolling back to the top) multiple border lines are displayed all over the screen.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{ static NSString *CellIdentifier = #"CellProgramm";
ProgrammTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
...
if([object.cellMessageArray[1] isEqualToString:#"wrapper"] || [object.cellMessageArray[1] isEqualToString:#"keynote"] || [object.cellMessageArray[1] isEqualToString:#"break"]) {
UIImageView *lineSeparator = [[UIImageView alloc] initWithFrame:CGRectMake(0, cell.bounds.size.height, 1024, 5)];
lineSeparator.image = [UIImage imageNamed:[NSString stringWithFormat:#"blind.png" ]];
lineSeparator.backgroundColor = [UIColor whiteColor];
[cell.contentView addSubview:lineSeparator];
}
else if([object.cellMessageArray[1] isEqualToString:#"standard"]) {
UIImageView *lineSeparator = [[UIImageView alloc] initWithFrame:CGRectMake(60, cell.bounds.size.height+4, 1024, 1)];
lineSeparator.image = [UIImage imageNamed:[NSString stringWithFormat:#"blind.png" ]];
lineSeparator.backgroundColor = [UIColor pxColorWithHexValue:#"eeeeee"];
[cell.contentView addSubview:lineSeparator];
}
}
Has anyone an idea?
When you scroll a tableview, the cells are reused (dequeueReusableCellWithIdentifier) to optimize performance. In the code above, a lineSeparator image view is added to the cell each time the cellForRowAtIndexPath method is invoked. If the cell is used 5 times, it will have 5 image views added.
One way address this is to remove the lineSeparator image view from the cell before it is reused. This is typically done in the cell's prepareForReuse method.
In the cellForRowAtIndexPath, add a tag to the lineSeparator image view (e.g., lineSeparator.tag = 100;
In your cell's class, implement the prepareForReuse method. E.g.:
-(void)prepareForReuse{
UIView *lineSeparatorView = [self.contentView viewWithTag:100];
[lineSeparatorView removeFromSuperview];
}

Need to remove border between cells in UITableView when using background image in iOS

I am having trouble removing the white border that appears between each cell inside my UITableView. I have tried using:
[myTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
inside my viewDidLoad method, but unfortunately that does not seem to work. I am using a background image for my cells to display, and it is the border between these images that I wish to remove. The image itself is the size of each cell, and the size of each cell I set in my CustomTableViewCell class as follows:
self.contentView.frame = CGRectMake(0, 0, 320, 80);
My cellForRowAtIndexPath method is as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"mycell";
MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
cell.ID.text = [[DataModel sharedInstance].ID objectAtIndex:indexPath.row];
cell.Name.text = [[DataModel sharedInstance].Name objectAtIndex:indexPath.row];
cell.Date.text = [[DataModel sharedInstance].Date objectAtIndex:indexPath.row];
[cell setBackgroundView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"Cell.png"]]];
return cell;
}
Despite everything I have done, I am seeing a clear white border between my cells in the table. I really would like to remove this. Can anyone see what I am doing wrong?
Inside Interface Builder - change the separator to None and then the separator color to clear color:
Setting UITableView separatorColor at viewDidLoad might be too early. Try viewWillAppear instead.

Resources