UITableviewCell values are changing - iphone

I am new in iPhone development. I am using UItableview for showing list.
And able to show all items successfully.
But they changed when I scroll tableview.
Please help me.

Try this code in cellForRowAtIndexPath method of tableview
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

Please give me more information to show your case clearly.
Have a look at this function first because all rows are generated from here!
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [recipes objectAtIndex:indexPath.row];
return cell;
}
Check whether each row is exist,unless just reuse it later.

Related

Loading UITableViewController from code

I am having issues with what I can only imagine is a very simple problem. I am loading a UITableViewController class called LocationViewController from one UIViewController:
LocationViewController *lvc = [[LocationViewController alloc] init];
[self.navigationController pushViewController:lvc animated:true];
In this class I have the 3 following method implemented:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CityCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.textLabel.text = #"Test";
return cell;
}
And I am getting the following error:
UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:'
I have had this error before when transitioning between ViewControllers using the StoryBoard this was because the CellIdentifier was not correct. I have no idea what I am doing wrong. I have tried loading a nib file with this ViewController but that throws the same error.
You have to allocate the cell. use this code.
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
use this code
- (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];
}
cell.textLabel.text = #"Test";
return cell;}
dequeueReusableCellWithIdentifier returns nil, if there is no object in the reusable-cell queue. check if cell is nil after calling dequeueReusableCellWithIdentifier, create a new UITableViewCell in that case.
dequeueReusableCellWithIdentifier is a cache of already created UITableViewCells with your identifier "CityCell"
If you use the code below, it tries to get a cell from the cache, and if it can't it will create one and then store it in the cell cache for later use.
You need the cache for large tables, as it drastically improves table performance when scrolling.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *MyIdentifier = #"CityCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
cell.textLabel.text = #"Test";
return cell;
}
Take a look at the table view programming guide to learn more about this:
Table Programming Guide

Redraw a UITableView

I need to literally redraw a UITableView upon an event in my code. But everything I've tryed doesn't seem to work. I've tried [self.tableView reloadData], I've played with the delegates. The goal I'm trying to achieve is to render a completely different UITableView, with differently formatted cells. So I need to redraw the table. Any help would be appreciated.
Heres my code:
...
if/else...
}
//Now I want to reload the tableView
[tableView reloadData]; //Isn't getting it done
NSLog(#"index = %i", index); //Always fires
tableView.delegate = self; // Didn't help
[tableView performSelectorOnMainThread:#selector(reloadData) withObject:nil waitUntilDone:NO]; // Also nothing
}
The point of what I'm trying to do is this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
if (segmentOptions == snacks) {
cell = [[TableViewCell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
NSLog(#"A");
} else {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
NSLog(#"B");
}
}
...
}
You possibly meant something like
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"cell-not-snacks";
if (segmentOptions == snacks) {
cellIdentifier = #"cell-snacks";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
if (segmentOptions == snacks) {
cell = [[TableViewCell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
} else {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
}
}
I think it would be worth looking at the method prepareForReuse in the UITableViewCell Class. This method is invoked just before the object is returned from the UITableView method dequeueReusableCellWithIdentifier:.
You can override this method on your custom UITableViewCell (do not forget including [super prepareForReuse]) in order to redraw whatever you need to redraw.
The problem is: if (cell == nil) {. I removed that and it works perfectly every time.

show a detaildisclosure button to the uitableview without custom cell

How to show a detail disclosure button to the uitableview without using the custom cell. I know its by using the accessoryType: method, but don't know how to implement that..pls help..
// 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] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
return cell;
}
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

How to set diff. image in cell in table

How to set different icon image for each cell in the UItable View.
In your tableview delegate:
- (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.imageView.image=[someDataClass methodThatReturnsAnImage];
cell.textLabel.text=[someDataClass methodThatReturnsAString];
return cell;
}

how to set the style of the TableView to "UITableViewCellStyleSubtitle"

how to set the style of the TableView to "UITableViewCellStyleSubtitle". so that i can use it to display as sub title in UITABLE
It's not a style of the UITableView, but of the UITableViewCell:
- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
... more stuff
return cell;
}