Problems with UITableViewCellStyleValue1 - iphone

I have the following code:
static NSString *CellIdentifier = #"Cell";
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
cell.textLabel.text = #"Publisher";
cell.detailTextLabel.text = #"This Is A Very Very Long String";
which results with the following look: (only first table row is relevant)
As it appears, the detail text is overlapping the "Publish" title so both strings are truncated.
What I wish is to have the "Publish" title neverc being truncated as follows:
Is this possible using UITableViewCellStyleValue1? I saw many posts suggesting to create a custom cell but is it really the only way?
Thanks,
Josh

You can try to set adjustsFontSizeToFitWidth property for cell's labels to YES and minimumFontSize property to an appropriate value. If it does not help - it seems you should use custom cells indeed.

I realize some time has passed since this post, so there have probably been updates to this functionality from the SDK. What I've found in Xcode v4.1 is UITableViewCellStyleValue1 now does what you wished. The textLabel.text doesn't truncate.
Using UITableViewCellStyleValue2 will truncate the title. Where the doc suggests
it functions as a heading or caption for the important information in
the more prominent, left-aligned detail text label.

Related

How to handle references to UITextFields in dynamic UITableView (iPhone)

I have a UITableView that is broken up into a user defined number of sections. Within each of these sections there are always 2 rows. Each of these two rows contains a UITextField which the user is able to edit.
What I need is a way of being able to access the data in these UITextFields at a later point. I was hoping it would be a simple problem however it's causing me a great deal of grief.
So far I have tried two approaches:
Attempt 1
I created two NSMutableArrays in which I added the UITextField objects to at index i (corresponding to the section it came from). I then tried to access the values by iterating through the array. This didn't work since the UITextFields kept getting wiped clean. Every-time I scroll down the table and the UITextField is out of view, when I go back it's contents have been wiped clean.
Attempt 2
I tried to get hold of the number of sections in the UITableView (this was fine). I then wanted to iterate through each section of the UITableView, recording the values in the rows of each. This is where I became unstuck since I'm not sure how to do this or if it's even possible.
I apologise if this is a naive question to ask, however I'm really struggling and would appreciate any advice.
Keep in mind that the text fields get reused as you scroll, so you don't really want to store references to them.
What you do instead, is to capture the information as it is entered. The easiest way to do this is to implement the textFieldDidEndEditing protocol method in the delegate.
The tricky part is figuring out which row the text field is in. The best way is to create a UITableViewCell subclass which has a NSIndexPath property. You can then set that when you configure the cell with tableview:willDisplayCell:forRowAtIndexPath:.
Then, in textFieldDidEndEditing, access the tableViewCell indexPath property through its superview. i.e.:
NSIndexPath indexPathOfParentCell = [(MyUITableViewCellSubclass *)self.superview indexPath];
Doing it this way allows you to know both the section and row of the cell.
Create your TextField in the cellForRow of the Table like so and give it a tag
UITextField * userField = [[[UITextField alloc] initWithFrame:CGRectMake(0, 12, self.view.frame.size.width -20, 20)] autorelease];
userField.tag = 1001;
userField.font = [UIFont fontWithName:#"HelveticaNeue-Bold" size:14];
userField.textAlignment = UITextAlignmentCenter;
userField.delegate = self;
userField.autocorrectionType = UITextAutocorrectionTypeNo;
userField.autocapitalizationType = UITextAutocapitalizationTypeNone;
userField.clearButtonMode = UITextFieldViewModeWhileEditing;
if (indexPath.row == 0)
[cell.contentView addSubview:userField];
then access the TextField like so:
UITextField *userField = (UITextField *)[[(UITableViewCell *)[(UITableView *)tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]] contentView] viewWithTag:1001];
Your "Attempt 1" should work OK if you keep the text field's text in your arrays rather than the text field itself. (Anything that tries to make a view or control into a data object has a good chance of going wrong.)
Whatever acts as a data source for your table view should be able to re-populate the scrolled cells according to section and row if the content is stored separately.

UITableView cell redraw causes text to overlap

I do following in cellForRowAtIndexPath
NSString *cellIdentifier = #"Cell";
LibraryCell *cell = (LibraryCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell != nil) {
[cellIdentifier release];
[self setItems:cell forRowAtIndexPath:indexPath];
return cell;
}
cell = [[[LibraryCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier] autorelease];
[self setItems:cell forRowAtIndexPath:indexPath];
return cell;
[self setItems:cell forRowAtIndexPath:indexPath]; changes only some UILabel's value inside the corresponding cell.
So now when I edit UITableView and delete let say the first row, the UILabel's value is no redrawn completly, i.e. the old value remains and new value is drawn overlapping the old one.
Why this happens and how to fix it ?
I guess you misunderstood the cell identifier concept.
It is only used to distinguish what you might call "stamps", used to print a cell's appearance on the screen. So you would most probably only need a single cell identifier.
It helps the system to cache instances of the "stamps". When cellForRowAtIndexPath is called, you only have to pick which kind of stamp you want to use. If you created an instance of the correct one before (i. e. you get one back when asking for it using the cell identifier string), you only need to change the label texts etc. and then return it. In real life this might be likened to one of those date stamps where you can change the date by turning the little knobs on the stamp. This is what you would do by assigning a new text to the label contained in the cell.
You instead seem to be creating a stamp for each index in your model by concatenating the string value, effectively creating as many instances as there are rows in your model. Apart from being unnecessary it might also cause memory pressure and stuttering, because it counteracts all sorts of optimizations the UITableView has.
I recommend you read up on Apple's documentation or see iTunes U (here)
for the Stanford courses on iOS development. It gets explained very clearly there.
First of all, You should use the same name for your cellIdentifier
Please refer to the UITableView Class reference
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html#//apple_ref/occ/instm/UITableView/dequeueReusableCellWithIdentifier:
If you cannot get a reusable cell, then create it. After that update your cell.
The code should be like this
LibraryCell *cell = (LibraryCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
// create a new cell
...
}
// Update cell
...
return cell;
Actually I figured out ! The problem was coming from setItems:forIndextPath method.
It was creating UILabel locally and releasing it. So every time it was drawn over the last text text. Making the UILable instance variable solved the issue.
PS. The code originally was not written by me, I'm just fixing it :)

Correct way to get UITableViewCell's text

Simple question though, but what is the correct way to get the text off my UITableViewCell?
With cell a UITableViewCell:
I know off cell.text, which gives exactly what I want, but is deprecated in iOS 3.0.
The only alternative I could find was cell.textLabel but this gives me the same (or almost?) as if I only used cell. Which is obviously not what I want.
So how can I get the text on my cell (what the user reads)? Or is it okay if I leave it as cell.text / will Apple accept it?
Simply use cell.textLabel.text = #"My Text"; and NSString *mytext = cell.textLabel.text as long as you are targeting 3.0 or above.

How to reuse uitableviewcell??

Every time when scrolling the cells reload shouldn't be trigged.
Apple knows: http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/TableView_iPhone/TableViewCells/TableViewCells.html%23//apple_ref/doc/uid/TP40007451-CH7-SW19
Yes you are correct. I'll elaborate on Steven's post of the Apple documentation a little.
For cells not to be reloaded, first look at this method:
TimeZoneCell *timeZoneCell = (TimeZoneCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
It says "give me the existing view that represents a table cell". This is a great saving because if the user is scrolling up or down quickly, creating views can be quite expensive.
Of course, you can't re-use a cell if one hasn't been created in the first place. So, you need to handle that scenario. That's where this code comes in:
if (timeZoneCell == nil) {
timeZoneCell = [[[TimeZoneCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
timeZoneCell.frame = CGRectMake(0.0, 0.0, 320.0, ROW_HEIGHT);
}
It says, "is the cell nil?", i.e., have we not yet created a table cell? It then goes on to create one. That cell will subsequently be be re-used.
The actual contents of the cells will change, for example the label or whatever. However you will continue to re-use the existing objects.

UITableView Scroll slowly

i got the problem in my apps, i have UITableView and it's scroll slowly, not as fast as the other application, even i didn't create any object or add subview in my table
this is my code :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *searchingIdentifier = #"searchingIdentifierCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:searchingIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:searchingIdentifier]autorelease];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.detailTextLabel.text =[[self.dataStoreByCategory objectAtIndex:indexPath.row]objectForKey:#"storeLevel"];
cell.textLabel.text = [listStore objectAtIndex:indexPath.row];
cell.imageView.image = [[self.dataStoreByCategory objectAtIndex:indexPath.row] objectForKey:#"storeIcon"];
}
somebody can help me.??
This mainly happens when the contents you are using in the cell is of large size.
In your case, the images you are using in the cells might be of very large size. You can check it by commenting the code and testing it.
To sort out the problem, make an array of thumb nail images to be displayed on the cell instead of the original images. You could find some methods to make the thumb nail of an image by reducing the height and width of image.
If you use the thumb nail images inside the tableview, your table would scroll faster. When the user select any table row, you can display the full image in your next view as required in your application.
Is your image that you put into UITableViewCell is big?
I think you can try to comment the line loading the image to see if it is faster. I don't see any problems in your code
What is the size of your images ?
In the same idea, what is the length of the text used ?
If your images are too big, it may slow down the scroll. The same for the text (but it needs a lot of text ^^)
Just try restart your device. Sometimes device tend to go slow if it is "on" for many days! Believe me!