I have a custom separator in every cell(added it in the IB), I want to delete/hide it based on a specific condition:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier= #"satellite";
SatellitesCell *cell= [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(!cell) {
cell =[[SatellitesCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if([[arrayofRadios objectAtIndex:indexPath.row] isEqualToString:#""]){
[cell.separatorImage removeFromSuperview];
cell.separatorImage = nil;
}
cell.satelliteName.text=[arrayofSatellitesName objectAtIndex:indexPath.row];
return cell;
}
When launching the view, all is okay, but the problem is when scrolling, the separator(the UIImageView) is displaying randomly in every cell.
If I understand you correctly, you want to use the same cell prototype but have it look different based on the data. If this is the case, then I wouldn't remove the separatorImage from the superview, just hide it (eg. cell.separatorImage.hidden = YES;). This way in the else case of that if statement, you can unhide it (eg. cell.separatorImage.hidden = NO;).
Or I suppose, if you have a specific reason for removing it from the superview, then just be sure to create an else statement for that if statement, and add it back to the cell (eg. [cell addSubview:cell.separatorImage];).
Related
Here is my code :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier=#"menucell";
MenuCell *cell = (MenuCell*) [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
if (cell==nil)
cell= [[MenuCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] ;
MenuModel *resturntent = (self.menuList)[indexPath.row];
cell.foodNameLbl.text = resturntent.MenuName;
cell.priceLbl.text = [NSString stringWithFormat:#"%# %#",#"$" , resturntent.MenuRate];
cell.foodImage.image = [UIImage imageNamed:#"full_breakfast.jpg"];
return cell;
}
While scrolling uitextfields values gets duplicates .
I need help to fix this issues
When you use dequeueReusableCellWithIdentifier, your cells will be reused. This means when the user scrolls the tableview, a cell which moves out of the screen will be reused to display the contents of a cell which is about to move onto the screen.
Even though this helps in saving memory, the problem it causes is the that the cell needs to be prepared for display before it is loaded with the content of the new cell.
In your case, it seems you need to maintain the values the user has entered in a textfield of a cell.
So to fix your problem, if there are not that many cells in the tableview, simply stop reusing the cell. Else whenever the user enters a value in the textfield of a cell, save it in an array (array of 'MenuModel' in your case). And fetch the value from from the model and set it to your textfield in the cellForRowAtIndexPath method like your have set values for other labels.
I'm using an array of strings where I set the detailTextLabel from. Initially all subtitles are set correctly but if I scroll the detailTextLabel disappears.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"personCell" forIndexPath:indexPath];
Person *person = [_persons objectAtIndex:indexPath.row];
cell.textLabel.text = person.name;
cell.detailTextLabel.text = person.phone;
// also tried setNeedsLayout but does not help
[cell setNeedsLayout];
return cell;
}
I'm using an iPhone 6 and iOS 8. I'm also using storyboard and set the UITableViewCell style to Subtitle.
OK, now that we've found the problem (with the nil phone number text on the person) you could solve it a couple of ways.
It seems that you don't want to set the text to blank. I imagine this is due to the fact that it lays out the cell in an odd way with the title pushed up to the top but nothing underneath it. Understandable.
So, you could create a custom UITableViewCell subclass. In it you can manage the layout yourself and if the number is nil lay it out one way and if it has a phone number lay it out a different way.
An easier way would be to use two different prototype cells instead.
In the storyboard create two prototype cells.
One with type Basic and give it a reuseIdentifier noPhoneNumberCell.
The other with type Subtitle and a reuse identifier phoneNumberCell.
Then in the code you can do something like this...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
Person *person = [_persons objectAtIndex:indexPath.row];
UITableViewCell *cell;
if (person.phone) {
cell = [tableView dequeueReusableCellWithIdentifier:#"phoneNumberCell" forIndexPath:indexPath];
cell.detailTextLabel.text = person.phone;
} else {
cell = [tableView dequeueReusableCellWithIdentifier:#"noPhoneNumberCell" forIndexPath:indexPath];
}
cell.textLabel.text = person.name;
return cell;
}
This will now create two queues of cells. One for people with phone numbers and one for people without.
This way you don't mix the two and so avoid the problem you are facing.
[cell.detailTextLabel sizeToFit];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"MyCell";
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil)
cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
Movie *movie = [self.movies objectAtIndex:indexPath.row];
cell.title.text = movie.title;
cell.subtitle.text = movie.subtitle;
cell.subtitle.numberOfLines = 0;
[cell.subtitle sizeToFit];
return cell;
}
I am calling reloadData from two places. One is from the end of a loadInitialData function, which is called from viewDidLoad.
A second one is being called from viewDidAppear, although this is inconsequential to my problem, because it existed before it and exists without it.
I initially load 3 rows of sample data, with titles and subtitles. Now what happens is my subtitle text is vertically centered when this window first launches. If I grab the table and scroll is high up, all of a sudden my [cell.subtitle sizeToFit] goes into action, and my text goes to the top vertically, which is desired.
So my issue is... why is the text vertically centered from the beginning? reloadData doesn't work either. When I return from adding a new row, all rows but the newly added row are vertically aligned to top as they should. The new row is incorrectly vertically centered.
Why doesn't this work? Everything seems good. New data is added etc. Via NSLog statements, I have verified numberOfRowsInSection is immediately called after reloadData is called.
So why does the aligning of the text vertically to the top not work?
Thanks!
This is probably because the UITableViewCell has not yet been layed out and so it does not have a size yet. Try doing the sizeToFit in this UITableViewDelegate method
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
I am not sure this will work, but it worth trying.
Add [cell setNeedsLayout]; before you return the cell so it will layout the cell before presentation.
So I've created a TableView as below, but I have quite an annoying problem.
When I come to this VC, I click Choose User, and I selected user Atoshum.
When I scroll down, this top cell goes off screen as I scroll through the bottom cells.
When I scroll back up, the cell has reverted to a default (or occasionally, takes the value of another cell).
I make the cells as such.
static NSString *CellIdentifier = #"Cell";
DMSDrugInstanceCell *Cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!Cell) {
Cell = [[DMSDrugInstanceCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Cell.chooseUserButton.tag = indexPath.row;
[Cell.chooseUserButton addTarget:self
action:#selector(performSegue:)
forControlEvents:UIControlEventTouchUpInside];
return Cell;
In the cell creation you dont ever set any values. All you do is set the tag and then add an event target. If you want it to keep the choose, you need to store/save that choice when it is made and then in the cell create, set it based on that saved value.
you are reusing table cell. So every time your cell reload then you need to set value in cell.
in cellForRowAtIndexPath method , you need to set value in cell according to index path.
This is because the UITableView do not create the new cells for total number of elements. Rather it re-uses the cells which are off the visibility. Hence you feel that your data is reset or getting reflected on some other cell.
The best approach is to store all your data in some Array (let it be tagDataArray) and then set your cells as
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
DMSDrugInstanceCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[DMSDrugInstanceCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
Cell.chooseUserButton.tag = [tagDataArray objectAtIndex:indexPath.row];
...
...
return Cell;
}
I have a UITable which shows the 10 most recent images from a web service. Each row has its own image. When the UITable first loads in the viewcontroller it doesnt show the first 4 images in the rows (screen is blank). If I scroll down the last 6 images appear....then if I scroll back the first 4 images that were NOT there originally DO appear and everything looks the way I wanted it to initially. My guess is its something do with the way cells are reused.
Here is my tableView code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
[cell.contentView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
[cell.contentView addSubview:[self.photoList objectAtIndex:indexPath.row]];
return cell;
}
What solved my issue was adding the following line to my method that deals with streaming the photos from the Web API:
[_tableView reloadData];
I noticed that if I moved the Table View Cell up or down in the its Table View at the Storyboard that changed the position of the invisible row as well. So I solved the problem by setting the Table View Cell as hidden.