How to fix Row in table in Uitableview in iphone - iphone

I am small basic module. I have 3 screen in this module. First screen is welcome message showing. Second screen is Table view which is contacting 3 Row with the title. Whenever user will select any row Full Text will be open for selected row. Last screen is containing one BACK button which will back to second screen (on tableview screen).
I have done this all till last screen. But when I am clicking on back button I am going on table view screen but I am seeing more than 3 row are showing on Simulator.
I wrote this code on button click function for backing to second screen (table view screen)
...
MyViewController *History_Back = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:[NSBundle mainBundle]];
[window addSubview:History_Back.view];
[self.view addSubview:History_Back.view];
....
And in TableView implementation class i am defining it in viewload function which is like below..
-(void)viewDidLoad {
NSLog(#"::::::::::::::");
tableList = [[NSArray alloc]initWithObjects:#"A",#"B",#"C",nil];
[super viewDidLoad];
}
And i want to show only 3 row. Where I should to modify code ?
My tableview coe is here.
-(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];
}
cell.textLabel.text=[tableList objectAtIndex:[indexPath row]];
NSLog(#"##############");
return cell;
}

By default, a UITableView will display as much rows as the tableView height allows (even if the content of the cell is empty). In order not to display more than 3 rows, you have two solutions :
either you resize your table view height to the appropriate height
or you can add tableView.separatorStyle = UITableViewCellSeparatorStyleNone; in the definition of your tableview. This means that you will have to add your own custom separator in the UITableViewCell design...

Related

Not refreshing screen in programmatically filled TableView

When I scroll down and up again my text in tableView will disappear.
And my code is:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [screenDefBuild.elementsToTableView count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
}
ScreenListElements *currentScreenElement = [screenDefBuild.elementsToTableView objectAtIndex:indexPath.row];
cell.textLabel.text = currentScreenElement.objectName;
currentRow++;
return cell;
}
- (void)viewDidLoad
{
[super viewDidLoad];
tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
[tableView setDataSource:self];
[self.view addSubview:tableView];
}
I also want to fill my table view to entire screen. (grey strap on the top).
I don't know what you're doing with this variable
currentRow++;
But whatever you use that for, i'd wager its breaking your code.
the UITableView will call cellForRowAtIndexPath every time a cell is about to appear on screen regardless of whether it has been on screen before or not. When you scroll down and then scroll back up this variable will have increased beyond the bounds of your data, hence you get empty cells.
You need to design this method in such a way that it can create any cell in the table view at any time. You can't rely on the order that the cells will be made and with scrolling you will have to make the same cell over and over again. Only use indexPath to figure out which cell you are currently supposed to be making.
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UITableView_Class/Reference/Reference.html
Answer for the second part of the question- grey strap. You are adding the table view to current view, so you should use the size property of self.view.frame but not the origin. You want this to be set to 0,0.
Change
tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
to
CGRect viewFrame=self.view.frame;
viewFrame.origin=CGPointZero;
tableView = [[UITableView alloc] initWithFrame:viewFrame];
As for the first part of your question- it's strange, as you seem to do everything properly. One thing i may suggest is to add [tableView reloadData]; in the end of viewDidLoad.

data in the table row , appears and disappears when we scroll up (or) scroll down the page

I have created a grouped table view like in the image below. but data in the table row , appears and disappears when we scroll up (or) scroll down the page , when we scroll up first 2 rows will go up (beyond the view) then if we scroll down both rows will take same value,, y its so? can any one help me
Thanks in advance.
like following code i am designing the table cell where CustomCellStudentData is class there i am creating like lable frames, text field frames
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
CustomCellStudentData *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomCellStudentData alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
switch(indexPath.section)
{
case 0:
switch (indexPath.row)
{
case 0:
{
tfText[0] = [[UITextField alloc] initWithFrame:CGRectMake(100,10, 200, 40)];
cell.accessoryView = tfText[0];
tfText[0].delegate=self;
tfText[0].placeholder =#"<Student Name>";
cell.primaryLabel.text =#"Student Name: ";
}
break;
case 1:
{
tfText[1] = [[UITextField alloc] initWithFrame:CGRectMake(100,10, 200, 40)];
cell.accessoryView = tfText[1];
tfText[1].delegate=self;
tfText[1].placeholder =#"<Student Age>";
cell.primaryLabel.text =#"Student Age: ";
}
}
}
UITableView reuses the cells so as to not bog down the memory. As soon as the two cells go up, they enter the pool of reusable cells. If the pool is not empty, dequeueReusableCellWithIdentifier: returns a cell from that pool (there is a dependency on the identifier here). As the table view needs cells for the new rows being scrolled in, it gets the cells from the pool and provides it to you for reconfiguration. So, the text field that you attached to the cell when configuring it for row 0 and 1 still remain unless you explicitly remove. Since we don't which cells we are going to get, we should reset all customizations that we do and configure it as a new cell. In this case, irrespective of the cell coming in, you should remove the text field as the accessory view first.
cell = [[[CustomCellStudentData alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
What Deepak said is correct.
Adding to his answer, in future, if you add any subView to cell view. Remove from the cell before you add anything on it. Use
for(UIView * view in cell.contentView.subviews){
[view removeFromSuperview]; view = nil;
}
before customizing the cell.
And yes, please increase your acceptance rate. Going back to the questions you asked before and accepting some answers should help.

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
}

iPhone app development : How can i get a table view in which whn i click on a cell a text view should expand just below that cell...?

How can i get a table view in which whn i click on a cell a text view should expand just below that cell it should look like clicking on a button an it will create a text view in between clicked button and its below button...
i m trying to create a pictorial view...
tabel view:
button 1 >
button 2 >
button 3 >
table view:
button 1 >
button 2 v
__________
.
.
. text view
.
----------
button 3 >
plz solve my problem i m new for iphone app development....
I dont think that this can be done the way u want it, because once the TableView is loaded it is loaded, and if even if you manage to push some extra cells in between, the Table View will not change until it is loaded again.
TableView is loaded in two scenarios i.e
1. When Loading the view
2. While scrolling within the TableView.
my advice is to show a popup window kind of thing...
- (UITableViewCell *)valueForSelectedRow {
UITableViewCell *cell = [self.AccountTable cellForRowAtIndexPath:[self.AccountTable indexPathForSelectedRow]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Get value
UITableViewCell *cell = [self valueForSelectedRow];
cell.frame = CGRectMake(cell.frame.origin.x, cell.frame.origin.y, cell.frame.size.width, cell.frame.size.height);
UITextview *txtview = [[UITextView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 25.0f)] autorelease];
[cell addSubview:txtview];
[cell sizeToFit];
//...
....
You might insert a row just under the selected one.
The steps are:
Create an instance of NSIndexPath for the new row
Update your datasource (insert a special object containing the information for your text view). Notice that the datasource object must be mutable (e.g. NSMutableArray).
Insert new row to the table
Sample implementation may look like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// 1
NSIndexPath *newRowIndexPath = [NSIndexPath indexPathForRow:(indexPath.row + 1) inSection:indexPath.section];
// 2 (datasource has to be mutable)
[self.datasource insertObject:[NSDictionary dictionaryWithObject:#"THE_TEXT_FOR_YOUR_TEXTVIEW"] atIndex:newRowIndexPath.row];
// 3
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newRowIndexPath] withRowAnimation:UITableViewRowAnimationBottom];
}
Notice that in addition to this code you will have to treat the "special" row differently in tableView:cellForRowAtIndexPath: method...
Cheers.
Michael.

UITextView in 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...
}