Issue about vertical text alignment in UITableViewCell (iPhone) - iphone

I use just UITableViewCell.
It occur only in real device(version 3.1.2) and in simulator(version 3.1.2) doesn't have any problem.
Thank you for your advice.
Here is my problem image

It seems like a bug to me, but a workaround is to set cell.detailTextLabel.text = #""; if you don't want any text to appear on the right side of the cell.
Or
If there are empty rows try filling it with #""
switch (indexPath.row) {
case 0:
cell.textLabel.text = #"";
break;
case 1:
[cell addSubview:label];
break;
case 2:
cell.textLabel.text = #"";
break;
}
The 0th row and 2nd row don't have any data in them. Hence filled it with #"".

Related

Show previously selected cells by default when view opens, UICollectionView

I am creating a seat map, there are few seats already booked and others can be, So, i need to show the already booked seats and available seats by different colors. Below is my code i am using to show selected and deselected cells, but how to make show already booked cells selected already.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [self.collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
NSLog(#"indexpath row:%d", indexPath.row);
int v;
NSString *cnt = [NSString stringWithFormat:#"%#",[arrSeats objectAtIndex:indexPath.section]];
v = [cnt intValue];
if(indexPath.item < v)
{
if(cell.selected){
cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"yelow_seat.png"]]; // highlight selection
}
else
{
cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"blue_seat.png"]]; // Default color
}
}else{
cell.contentView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"blank_seat.png"]];
}
return cell;
}
Below is the code i am trying to use to add values for each section. Each time array add values for a section it adds it into dictionary then clears the array and again next time the array add values for next section and and put to dictionary, but dictionary not holding the value when array objects removed.
arrStatus = arrSeatsStatus;
[seatsDict setObject:arrStatus forKey:[NSString stringWithFormat:#"%d",i]];
i++;
[arrSeatsStatus removeAllObjects];
I am having an array that holds the status of all seats as on or off, but how to implement it am not getting. Please guide for above.
Thanks in advance.
check this link for cell highlighting
http://developer.apple.com/library/ios/#documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/CreatingCellsandViews/CreatingCellsandViews.html
or do one this create a label or imageview for cell and do the checking whether the corresponding element is matching with your array element and then check their status ,if its matching then put some image or assign color over that item if its not matching leave it empty.
If it not working please let me i will help you

cell labels are not refreshing in table

I am attempting to call a reloadData on my table's rows on a viewDidAppear method access. However, my cells are not refreshing their values and I cannot figure out why, as it seems everything is being accessed in the order it is suppose to. To make matters more odd, 1 row actually does refresh, but none of the others do.
Here is my code...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Set up the cell...
static NSString *CellWithIdentifier = #"Cell";
UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellWithIdentifier];
NSLog(#"generating cell contents");
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellWithIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = [_tableGroup.options objectAtIndex:rowcount];
rowcount++;
//label for currently selected/saved setting
_currentSetting = [[UILabel alloc] initWithFrame:CGRectMake(160, 8, 115, 25)];
[_currentSetting setFont:[UIFont systemFontOfSize:14]];
_currentSetting.backgroundColor = [UIColor clearColor];
_currentSetting.textColor = [UIColor blueColor];
_currentSetting.textAlignment = NSTextAlignmentRight;
[cell.contentView addSubview:_currentSetting];
NSLog(#"added new label to cell");
}
//depending on the setting, set the label in the cell to what is currently selected
if (indexPath.section == 1 && indexPath.row == 0) {
_currentSetting.text = [NSString stringWithFormat:#"%# %#",[settings.mapDistance stringValue], NSLocalizedString(#"MILES_IDENTIFIER", nil)];
NSLog(#"setting map distance label: %#", settings.mapDistance);
}
else if(indexPath.section == 1 && indexPath.row == 1)
{
_currentSetting.text = [NSString stringWithFormat:#"%# %#",[settings.maxCustomers stringValue], NSLocalizedString(#"ITEMS_IDENTIFIER", nil)];
NSLog(#"setting max customers: %#", settings.maxCustomers);
}
else if(indexPath.section == 2)
{
_currentSetting.text = [NSString stringWithFormat:#"%# %#",[settings.maxProducts stringValue], NSLocalizedString(#"ITEMS_IDENTIFIER", nil)];
NSLog(#"setting max products: %#", settings.maxProducts);
}
return cell;
}
based on this code, i get this output with my NSLOGS.
this is the first run of the cells when the view is created. It generates 4 cells, puts labels in each cell, and in 3 of those labels, puts in a value.
generating cell contents
added new label to cell
generating cell contents
added new label to cell
setting map distance: 15
generating cell contents
added new label to cell
setting max customers: 250
generating cell contents
added new label to cell
setting max products: 150
at this point i have clicked a row, went to a different screen, and have now returned. as you can see, map distance is different. although no change is displayed, even though the code to change the label's text is accessed during the reload process.
reloading data
generating cell contents
generating cell contents
setting map distance: 25
generating cell contents
setting max customers: 250
generating cell contents
setting max products: 150
again, I'm at a loss because the last row DOES refresh correctly. But none of the others do.
Thanks
When you reload your tableView, the cells already exist and are dequeued from the tableView, so the condition if (cell == nil) returns false, and the cell creation code is not executed.
In that cell creation code, you are assigning a value to _currentSetting and then proceed with the acode assuming that value is correct. However, when the cell creation code is not executed, that value points to the latest created cell, and thus, it won't update.
To fix this: make _currentSetting a local variable and change the code to look like this:
(You don't really need to make it a local variable, but it's more appropriate because you don't really need a reference to the last label you created after you leave this method)
UILabel *_currentSetting = nil;
if (cell == nil) {
_currentSetting = ...
_currentSetting.tag = 123;
}
else
_currentSetting = [cell.contentView viewWithTag:123];
...
The problem here is that the second time (when you are reloading the view ) the _currentSetting is not having a valid memory .So it is better to implement a custom cell and do the job
Better refer this an excellent guide
The second time around, you can see that "added new label to cell" isn't being called, so you're re-using an old tableViewCell.
Note that you're not setting _currentSetting when re-using a cell, only when creating a new cell. So _currentSetting is set to the last new cell that was created, most likely the last cell in the table.
You need to make sure to set _currentSetting to the correct label (maybe by using viewWithTag: or something similar).
(e:f;b)

TableViewCell's textLabel value returns to 1 when cell is scrolled out of the view

i have a table view in which i can add 1 or subtract 1 to the value of my cell.textLabel.text but when i switch views and return or scroll a cell out of the view, and when it comes back into view, the textLabel's value returns to 1 which is the original starting point! Please help! Here is the code to add and subtract 1:
- (IBAction)addLabelText:(id)sender{
cell = (UITableViewCell*)[sender superview]; // <-- ADD THIS LINE
cell.textLabel.text = [NSString stringWithFormat:#"%d",[cell.textLabel.text
intValue] +1];
}
- (IBAction)subtractLabelText:(id)sender
{
cell = (UITableViewCell *)[sender superview];
if ( [[cell.textLabel text] intValue] == 0){
cell.textLabel.text = [NSString stringWithFormat:#"%d",[cell.textLabel.text intValue] +0];
}
else{
cell.textLabel.text = [NSString stringWithFormat:#"%d",[cell.textLabel.text intValue] -1];
}
}
This is happening because, the cells will be re-used on scrolling. The table view's datasource method will be invoked, hence the values get reset to the original value. You can maintain an array of NSNumbers as a datasource to the tableview (is, in cellForRowAtIndexpath: , set the text fo the cell label from the array). Each time you need to add or subtract, do it the corresponding NSNumber obj and re-load the tableview.
Seems like you are allocating a new cell each time.. and not using the cell re-usablility method.
In your case, when you are performing arithmetic actions to your previous values and you don't have an array to store previous values. The easiest way to fix this is make your Cell-Identifier unique. (something like #"Cell-%d",indexPAth.row)
Note: However, more efficient way would be to save your result in the array you are populating your data from, without making you Cell-Identifier unique.
You are not updating your data modal. That is why it is taking the original content value.
After change the cell text value reload the tableview [self.tableview reloadData]

UITableView cell image problem

I am trying to import an image in just cell number 1 and 2 ! , but I the result is my image will show in last cell ! I do not know why !! this is the picture that shows my situation :
// Configure the cell.
NSUInteger row = [indexPath row];
cell.textLabel.text = [titles objectAtIndex:row];
cell.detailTextLabel.text = [subtitle objectAtIndex:row];
switch (indexPath.row) {
case 0:
cell.imageView.image = [UIImage imageNamed:#"new.png"];
break;
case 1:
cell.imageView.image = [UIImage imageNamed:#"new.png"];
break;
}
return cell;
}
Either in your tableView:cellForRowAtIndexPath: set the imageView's image to nil before conditionally checking to set the new image or implement prepareForReuse on your cell subclass and set all of the cell's views values to nil.
This will ensure that reused cells are 'clean' before they're brought on screen.
Alternatively you could edit your switch to look like:
switch (indexPath.row) {
case 0:
cell.imageView.image = [UIImage imageNamed:#"new.png"];
break;
case 1:
cell.imageView.image = [UIImage imageNamed:#"new.png"];
break;
default:
cell.imageView.image = nil;
break;
}
The first thing that it makes me think of is that the first cell gets somehow recycled and used for the last cell. You can try to set the image view to nil for every cell and set it just in the first two cells. Should be something like this:
cell.imageView.image = nil;
Hope it helps! ;D
You might have used "static NSString *reuseIdentifier = #"Identifier" ", this means that all cells will be considered same by this reuseIdentifier. Only visible cells would be different so for example, if there are 5 visible cells on device, then only 5 new cells would be allocated for cell, and then if you scroll down or up, these 5 cells will be reused if you specified reuseIdentifier statically.
I would suggest to make cell uniquely identified by reuseIdentifier, change the above line to "NSString *reuseIdentifier = [NSString stringWithFormat:#"cell%d",indexPath.row]" This would solve the issue.
Hope this helps.
Your cells are reused by the tableview to save memory. You have to tell the cell not to use an image on every other cell. Modify the switch command like this:
switch (indexPath.row) {
case 0:
cell.imageView.image = [UIImage imageNamed:#"new.png"];
break;
case 1:
cell.imageView.image = [UIImage imageNamed:#"new.png"];
break;
default:
cell.imageView.image = nil;
break;
}
Sandro Meier
try this it might help you
case 0:
cell.imageView.image = [UIImage imageNamed:#"new.png"];
break;
case 1:
cell.imageView.image = [UIImage imageNamed:#"new.png"];
break;
default
cell.imageView.image=nil;

Trouble with UITableViewCellAccessory (Checkmark/None toggle)

edit
Well now don't I feel like an idiot. Until now, I haven't been paying any attention to whether or not a cell gets a checkmark. Somehow, I had UITableViewCellAccessoryCheckmark and UITableViewCellAccessoryNone flipped, so it was turning off when I wanted it on and turning on when I wanted it off. Reading code properly really does help with debugging...
/edit
Below is the code I was having so much trouble with.
From tableView:didSelectRowAtIndexPath:
if (i.need == 0) { // item not needed - hide (#) and turn on checkmark
i.need = 1;
cell.textLabel.text = [NSString stringWithFormat:#"%#", i.name];
cell.accessoryType = UITableViewCellAccessoryNone;
} else if (i.need < 0) { // item not needed - hide (#) and turn on checkmark
i.need = -i.need;
cell.textLabel.text = [NSString stringWithFormat:#"%#", i.name];
cell.accessoryType = UITableViewCellAccessoryNone;
} else { // item not needed - show (#) and turn off checkmark
i.need = -i.need;
cell.textLabel.text = [NSString stringWithFormat:#"%# (%d)", i.name, -i.need];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
The following is what you get for making this mistake.
Tap row with checkmark: (#) hidden, checkmark removed
Tap row without checkmark: (#) shown, checkmark not set until next tap
I'm not sure how I made the mistake of not connecting comment and accessory, but there you go. This is what it should look like:
if (i.need == 0) { // item not needed - hide (#) and turn on checkmark
i.need = 1;
cell.textLabel.text = [NSString stringWithFormat:#"%#", i.name];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else if (i.need < 0) { // item not needed - hide (#) and turn on checkmark
i.need = -i.need;
cell.textLabel.text = [NSString stringWithFormat:#"%#", i.name];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else { // item not needed - show (#) and turn off checkmark
i.need = -i.need;
cell.textLabel.text = [NSString stringWithFormat:#"%# (%d)",
i.name, -i.need];
cell.accessoryType = UITableViewCellAccessoryNone;
}