In-place editing of text in UITableViewCell? - iphone

I know this has to be a really simple question, but I am having trouble finding it in a simple place where there aren't so many other things going on that I can isolate the specific behavior.
I know I need to add a UITextField as a subview to the UITableViewCell to make it editable. What is the best way to do this? (I see an example in UICatalog, but I am confused with all of the extra things it is doing with the Dictionary.)
Again, please excuse the ignorance of this question. A pointer to sample code or a screencast would be much appreciated.
Thanks in advance,
Alan

First, define about UITextField.
txtField=[[UITextField alloc]initWithFrame:CGRectMake(5, 5, 320, 39)];
txtField.autoresizingMask=UIViewAutoresizingFlexibleHeight;
txtField.autoresizesSubviews=YES;
txtField.layer.cornerRadius=10.0;
[txtField setBorderStyle:UITextBorderStyleRoundedRect];
[txtField setPlaceholder:#"Type Data Here"];
Then write the code below in cellForRowAtIndexPath method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
}
[cell addSubview:txtField];
return cell;
}

You can create your custom UITableViewCell and put whatever you want in it.
Then you simply have to use it in your UITableView.
If you are worried about what to use as UITextField delegate for your textfield to manage all the delegate methods, IMHO I'll put it in the UIViewController that handle the UITableView, so that you can update the DataSource once that the textfield has been edited.

Related

How to place a Rounded Rect UIBUTTON on UITableViewCell programmatically

I was trying to place two uibuttons on uitableviewcell programmatically.. but unable to achieve that>. somehow i managed to get the buttons through some sample project in which u can draw a nib for cell.. but i was unable to set a selector function for buttons.. in other example i can place a uiinfodark type button but when i change the type to rounded rect the button gets disappeared and i am unable to figure out why? I might be doing some silly mistake but I am really kinda stuck with it right now so I'll be very much thankful to all of you for helping in this regard.
You will need a UITableViewDataSource
http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDataSource_Protocol/Reference/Reference.html
Then something like this
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CellIdentifier";
// Dequeue or create a cell of the appropriate type.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.accessoryType = UITableViewCellAccessoryNone;
}
// Get the object to display and set the value in the cell.
[cell addSubView: YOUR_BUTTON];
[cell addSubView: YOUR_OTHER_BUTTON];
return cell;
}

UITableViewCell ShouldIndentWhenEditing Issues

Im having issues the editing mode of the tableview. When we set the table to edit mode it indents the row cells to the right for edit field on the right. I want to stop this from happening. I have set the cell.shouldIndentWhileEditing = NO; but this doesn't change anything.
Another thing to note is this cell is programmatically built on the fly e.g.
static NSString *CellIdentifier = #"ListingCustomCell";
UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 34) reuseIdentifier:CellIdentifier];
}
cell.shouldIndentWhileEditing = NO;
//SETUP CELL FIELDS
//return cell;
Any ideas on what im doing wrong?
Thanks
Perhaps your UITableViewDelegate is returning YES in - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath. If the delegate method is implemented, it'll override the value on the cell.
Did you read this part of the documentation?
This property has an effect only on table views created in the grouped
style (UITableViewStyleGrouped); it has no effect on
UITableViewStylePlain table views.
Maybe that’s your case.

UITableView not scrolling smoothly...(iPhone SDK) ..!

UITableView not scrolling smoothly...(iPhone SDK) ..!!
I have implemented UITableView DataSource and Delegate methods in an individual separate classes.(one for delegate and one for datasource) in main program i write only:
//assume that all objects are allocated
ObjTableView.dataSource=ObjDataSource;
ObjTableView.delegate = ObjDelegate;
[self.view addSubView: ObjTableView];
when i run this code , UITable view appears but when i try to scroll it, it doesn't scroll smoothly.
I have also checked that UITableViewCell doesn't redraw once the cell is initialized.
can any one tell me why this happens ? How can i solve this problem ??
From comments:
ListDataSource *ObjListDataSource = [[ListDataSource alloc]initWithArray:[[sender object] valueForKey:#"List"]];
ListDelegate *ObjListDelegate = [[ListDelegate alloc]initWithArray:[[sender object] valueForKey:#"List"]];
tblList = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
tblList.dataSource = ObjListDataSource; tblList.delegate = ObjListDelegate;
[self.view addSubview:tblList]; [tblShopList release];
More from comments:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = [NSString stringWithFormat:#"%i",indexPath.row];
UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0,0,320,100) reuseIdentifier:CellIdentifier] autorelease];
//custom cell code
}
return cell;
}
More Information:
I have used NSNotification which notifies to current class when parsing is complete, after receiving notification , current class method calls DataSource, Delegate methods (which is defined in a separate class file).
So UItableViewCell customization (which is in ListDataSource) and table view(in current class) both are in different classes.
A problem is
NSString *CellIdentifier = [NSString stringWithFormat:#"%i",indexPath.row];
The id needs to be the same for all cells of the same class, otherwise you never reuse them. As you can see in most examples, it is indeed a constant in most (all?) cases.
Little explaination on the reuseIdentifier: every time a cell gets out of screen, you can reuse it instead of creating a new one. To reuse it, you need a cell in queue with the same identifier as the one you pass to dequeueReusableCellWithIdentifier. The way you did, the cells are never reused, because each id is unique (they may or may not be reused in case a row reappears on screen, depending on queue size, which is not configurable AFAIK). This is why personalization of the cell should happen OUTSIDE the "cell == nil" block. Long story short, you are using the reuseIdentifier not as intendend.
I think Michele is correct, but I would also add that it looks like you are doing your cell customization where the cell gets created. What you should be doing is something more like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *CellIdentifier = #"CellIdentifier";
UITableViewCell *cell = (UITableViewCell)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectMake(0,0,320,100) reuseIdentifier:CellIdentifier] autorelease];
//custom REUSABLE cell code here, e.g. text color, etc.
}
NSString *cellText = [dataArray objectAtIndex:indexPath.row]; //assuming you have a simple array for your data
cell.textLabel.text = cellText;
return cell;
}
I would also add that I'm not sure why you are able to run the app with the code you have here, since UITableViewCell cell = ... is an invalid initializer. It should be UITableViewCell *cell = ....
It would be helpful to see how you are customizing your cell, since without that it's hard to see what's happening.

UITextField inside of UITableViewCell will not activate on iPad but works on iPhone

I have a UITextField inside a UITableViewCell. It will not activate on the iPad (but it works fine on the iPhone) no matter what I try. Tapping on it and telling it to become the firstResponder both fail. The odd thing is that if I take the exact same code and move it to another view controller in my app it executes just fine. This makes it seem as if there is likely a problem in the parent UITableViewController but I can't find anything obvious. I'm hoping that someone out there has experienced a similar problem and can point me in the right direction.
Below is the sample code that works fine when I move it to a new project or put it in a new view controller launched immediately by my app delegate:
// Customize the appearance of table view cells.
- (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] autorelease];
}
if (indexPath.row == 0) {
UITextField *nameText = [[UITextField alloc] initWithFrame:CGRectMake(5, 5, cell.contentView.frame.size.width, cell.contentView.frame.size.height)];
nameText.delegate = self;
nameText.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:nameText];
[nameText becomeFirstResponder];
[nameText release];
}
// Configure the cell...
return cell;
}
Help!
As is typical for me if I just ask the question I find the answer a few moments later. It turns out I did have a view several steps before that had asked for the first responder but not resigned it. Live and learn!

How to display multiple columns in a UITableView?

I want to display multiple columns in a UITableView.
For Example:
TableView
FName LName Age
----- ----- ---
Abby Michale 34
I think the correct way of doing this is UICollectionView. Started with UITableView and finally failed at some point due to unexpected behavior while trying to implement scrolling both left-right and up-down.
Please check this awesome post for a complete and up-to-date solution:
http://www.brightec.co.uk/blog/uicollectionview-using-horizontal-and-vertical-scrolling-sticky-rows-and-columns
Result will be like this:
You can define a custom cell in IB, which will contain 3 labels and in function:
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *DetailCellIdentifier = #"DetailCell";
UITableViewCell *cell = [aTableView dequeueReusableCellWithIdentifier:DetailCellIdentifier];
if (cell == nil) {
NSArray *cellObjects = [[NSBundle mainBundle] loadNibNamed:#"DetailCell" owner:self options:nil];
cell = (UITableViewCell*) [cellObjects objectAtIndex:0];
}
// setup your cell
}
when you define the cell in IB give each label a tag, so when you need to put a text there you can retrieve it by tag like this:
label = (UILabel *)[cell viewWithTag:NAME_TAG];
label.text = myObject.name;
and repeat this with other 2 labels. The tag is a unique number.
Put this code instead //setup your cell comment
I have created UIGridView. I believe your problem can be solved using the same technique.
You can learn the source code of UIGridView. The code is really short.
Bit late to the party, but we've open sourced our fully featured table component:
https://github.com/flexicious/iOSDataGrid
Some screenshots:
http://www.ioscomponents.com/Home/IOSDataGrid
Feel free to use!
Create a custom UIView subclass containing, say, three UILabel elements as subviews. Then set the cell's contentView property to this custom view.
add three labels as sub viewsinto uitableview cell's content.then assign apprrpriate values to it
eg:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
//frame should be set accordingly(means should be framed accordingly).
UILabel *l1=[[UILabel alloc]initWithFrame:CGRectMake(5, 5, 100,100)];
UILabel *l2=[[UILabel alloc]initWithFrame:CGRectMake(110,10,100,80)];
UILabel *l3=[[UILabel alloc]initWithFrame:CGRectMake(115,60,100,50)];
[cell.contentView addSubview:l1];
[cell.contentView addSubview:l2];
[cell.contentView addSubview:l3];
return cell;
}