Values are getting cleared from UITableViewCell while scrolling - iphone

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.

Related

Custom CellIdentifier is Null When Using Search Display Controller

In my tableview have custom cells that I initialize from a UITableViewCell class. I have sections for first letters of records and have an indexPath that is being created dynamically.
I wanted to add a search display controller to my tableview. So I did, created all methods to filter data. I am sure that my functions are working well because I am printing array count to screen for search results.
My problem is that the first time view loads, the data is on the screen. But when I hit the search input and type a letter, than I get 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:' error. After I used a breakpoint I saw that my custom cell is nil after searching. Data is exist, but cell is not being initialized.
Here is the code I use for custom cell initializing:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ObjectCell";
SpeakerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSDictionary *myObject = [[sections valueForKey:[[[sections allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.label1.text = [myObject objectForKey:#"myValue"];
return cell;
}
I believe I made a mistake when putting controls in IB. So I added screenshots of objects:
Connections inspector for my table view
Connections inspector for my search display controller
EDIT: Problem is actually solved, I have used a UISearchBar instead of Search Display Controller but I guess this issue remains unsolved. So I'm willing to try any ways to make it work.
As of here search display controller question,
you need to access the self.tableView instead of tableView:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"CellId"];
// do your thing
return cell;
}
For those using iOS 5 and StoryBoards, you would want to use the following method instead of initWithIdentifier:
initWithStyle:(UITableViewCellStyle)stylereuseIdentifier:(NSString *)reuseIdentifier
Example:
NSString *cellIdentifier = #"ListItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
I'm not sure about how this should work in storeboarding.
But normally you would check if the [tableView dequeueReusableCellWithIdentifier:CellIdentifier] returns a cell.
Because if the cell in not loaded before or there aren't any cells to reuse you will have to create a new cell:
SpeakerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[SpeakerCell alloc] initWithIdentifier: CellIdentifier];
}
Also when in declaring local variables in Objective-C we tent not to capitalize the first letter.
I had the same issue, with custom cells (built in Storyboard) not being drawn as soon as the first letter was put in the search field. The search was successful however.
Finally I found a good tutorial from Brenna Blackwell suggesting to configure manually the cell drawing in the corresponding subclass of UITableViewCell, adding UILabels and other items.

UITableView issue regarding tick mark and reloading cells

I have a tableview with 5 sections and variable number of rows in each section with a custom cell (for check and uncheck images on a cell). When I select a row in first section, tableview also selects any one of the row in the last section.
And when I scroll up and down, the checked rows are getting shifted randomly with in the sections.
The issue is that it's not there when I remove one section in the tableview. I.e even number of sections.
Can anyone help me?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *kCustomCellID = #"MyCellID";
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:kCustomCellID];
if (cell == nil)
{
cell = (CustomCell *) [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID]autorelease];
}
That is the code that I used.
I'm sure somewhere I did a mistake. But, I'm not able to find it.
this is because of reusing of cells in the table, read more about table reusing methodology.
To go in detail the changes made to a cell will be copied to others since the cells are reused to viewable cells.
We will need to see some code to properly diagnose the problem, specifically the implementation of - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {.
Are you correctly reusing the cells as specified in apples docs i.e.
static NSString *cellIdentifier = #"MyAwsomeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
}

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

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