UITextView in iphone - iphone

i am creating textview(editable) through coding in uitableview in cellforrowatindexpath delegate. textview is showing in every row correctly. the problem is that when i am enter text in textview and scroll the tableview, then text disappear from textview. if anyone has any idea?

When you go to edit the text in the textview, the text scrolls up so that the top of the textview is where the new text will appear when typed. Try swiping down on the textview to reveal the previous info.

Make sure you are adding a different textview to each of the different reusable cells. If you have, say, 6 cells visible at any time, then you need 6 different textviews.
It sounds like when you scroll, your textview gets used for another cell.
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
MyTableCell *cell = (MyListTableCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
cell.textView = [[[UITextView alloc] initWithFrame:/** put appropriate rect here **/] autorelease];
// create other cell structures you need
}
// Set up the cell...
}

Related

how to cascade uitableview's width to its custom table view cell?

I have a certain sized UIView used as a container of a UITableView. I use a subclass of UITableViewCell in my table view.
The problem is that my custom table view cell can not get the proper width from the UITableVIew.
This is how I create the table view
categoryListView = [[UITableView alloc] initWithFrame:categoryListContainer.bounds style:(UITableViewStylePlain)];
This is how I create the custom table view cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == categoryListView) {
static NSString *CellIdentifier = #"MenuCategoryListIdentifier";
MenuCategoryListCellView *cell = (MenuCategoryListCellView*) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
// create dynamically
cell = [[MenuCategoryListCellView alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:CellIdentifier];
}
....
I wish to be able to get the correct cell bounds to in my MenuCategoryListCellView's initWithStyle method to draw my UI controls. But I don't know how to pass the value down.
Help will be appreciated!
Leo
The system will send the layoutSubviews message to your cell when the cell is resized. Override that method to lay out the UI controls according to the cell's current size.

How to Align the text of my label?

I want to align the text of my label to right aligned. But the problem is my label is in UITableVIewCell and i am using 'Value1' of UITableViewCell. So i want to align detailText label to the right side of the cell but sometimes the labels is in two or three lines so in that case i want the next line of the label to be left aligned so that it looks the next line is starting from left.
Please help!!!!
You can use custom tableview cells. Add a UITableViewCell class to your project and call it CustomCell, create a TableViewCell in Iterface Builder. Add two Labels, One right aligned called "label1", the other one left aligned called "label2", or do what ever you want, Then you can use it in your main class like this:
#import "CustomCell.h"
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.label1.text = #"some rightAlignedText";
cell.label2.text = #"some leftAlignedText";
return cell;
}
You need to look at a different cell type or create your own custom one because as far as I know, the way each table cell is laid out is set in stone

Values are getting cleared from UITableViewCell while scrolling

I have placed three buttons over UITableViewCell and its background is set to an unselect radio button image.
While clicking the button I'm again setting the background image as select button.
In a row one select button should persist but while scrolling UITableView all the selected button images are getting cleared
Can somebody please give me any idea how to do this in this way or in other way ?
You probably are not reusing the cells correctly. When you scroll, a previous cell is reused by the UITableView, but you don't change its contents correctly. Here is a sample code for ur reference
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier] autorelease];
}
cell.textLabel.text = [urDataArray objectAtIndex:indexPath.row];
return cell;
}
save the state of you button in table datasource.
set that value to your button. table is reloading each displayed row while scrolling.

Hierarchical UITableview

In my first view I have a UITableView with 5 records. In UITableView cells are holding two different components. One UILabel is text and another UILabel shows value for that cell.
On selection of this UITableViewCell user is navigated to another view where I have given a picker controller for changing the value of 2nd label in the table view for the selected row. I reload the UITableView data on -viewWillAppear but this overlaps the text on lables as many times I visit it.
Can anyone give me relevant example of hierarchical tableview where UItableViewCell's right section is dependant on selection of picker from next view.
I think you are adding the labels on cell.contentView everytime you reload the table, but are not removing the previous ones. Thats why the overlapping is happening.
try to remove the lables
- (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];
}
for (UIView * view in cell.contentView.subviews) {
[view removeFromSuperview];
view = nil;
}
// add your labels
}

update uitableview cell object

i have a tableview that contains a uiwebview in its first row. i would like to change this tables' webview object with new one when user clicks a button. i am using the code given below but it does not work fine. older object is there and the newer one is over it although i recreate the webview. how can i remove the older one from cell?
thanks...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSLog(#"NİL.......");
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
} else {
NSLog(#"NOT NİL.......");
}
[cell addSubview:webView];
return cell;
}
Since you are using "dequeueReusableCellWithIdentifier", your should reconfigure the cell each time when the cell is displayed.
Please remember that the cells with same identity will be reused. It's better to set the same identity to one particular type of cells, typically cells with same subviews and layout.
This is sample for how to load data and reconfigure a cell: http://code.google.com/p/tweetero/source/browse/trunk/Classes/MessageListController.m
Here is a tutorial to get familiar with UITableView:
http://developer.apple.com/library/ios/#documentation/userexperience/conceptual/TableView_iPhone/CreateConfigureTableView/CreateConfigureTableView.html