Evaluate each row of UITableView separately iPhone - iphone

I want to show a picture if each row starts with a certain character, let's say "&" in the below example. The code below seems to only work if the first row doesn't start with '&'. If the first row has '&' all of the rows will have the picture. What's wrong?
- (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];
}
if ([[[array objectAtIndex:(indexPath.row)] substringToIndex:1] isEqualToString:#"&"])
{
cell.textLabel.text = [array objectAtIndex:(indexPath.row)];
cell.detailTextLabel.text = [array objectAtIndex:(indexPath.row)+1];
cell.imageView.image = [UIImage imageNamed:#"picture.png"];
return cell;
}
else
{
cell.textLabel.text = [array objectAtIndex:(indexPath.row)];
cell.detailTextLabel.text = [array objectAtIndex:(indexPath.row)+1];
return cell;
}
}

Since the first cell may be reused later on, you have to "reset" its state on every call of cellForRowAtIndexPath. Try adding cell.imageView.image = nil after cell.detailTextLabel.text in the else block.

Related

UITableView redraw issue on scrolling

I have a grouped UITableView that I populate from a list.
On some rows I don't want to have disclosure and on some I need to add label.
But is somehow mix something and add labels on wrong rows and display disclosures at each row.
What am I doing wrong here?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.accessoryView = [[ UIImageView alloc ]
initWithImage:[UIImage imageNamed:#"customdisclosure.png" ]];
}
NSDictionary *dictionary = [_list objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:#"Items"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
if([cell.textLabel.text isEqualToString:#"with label"])
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.detailTextLabel.textColor = [UIColor blackColor];
cell.detailTextLabel.text = #"label...";
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else if([cell.textLabel.text isEqualToString: #"No disclosure" ])
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
in your else if clause, you are not clearing the cell.detailTextLabel's text on the reused cell. Set it to nil and you will be fine.
cell.detailTextLabel.text = nil;
You will also need to hide the accessoryView in the else if clause, and unhide it.
cell.accessoryView.hidden = YES;
Overall, I would consider subclassing UITableViewCell so you can override prepareForReuse to reset your cell for the next cellForRowAtIndexPath call.
Guess the issue is with using reusable identifiers. Use different cell identifiers for the cells you do not want accessoryView.
static NSString *CellWithDisclosure = #"CellID_WithDisclosure";
static NSString *CellWithNoDisclosure = #"CellID_NoDisclosure";
NSDictionary *dictionary = [_list objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:#"Items"];
NSString *cellValue = [array objectAtIndex:indexPath.row];
if([cell.textLabel.text isEqualToString:#"with label"])
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithDisclosure];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
else if([cell.textLabel.text isEqualToString: #"No disclosure" ])
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellWithNoDisclosure];
cell.accessoryType = UITableViewCellAccessoryNone;
}
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellID] autorelease];
}
cell.textLabel.text = cellValue;
return cell;

how to add to cell into tableview

is it possible to add 2 cell.textlabel into the table view. As i want trying to display 3 different lines.
Here the code i did :
- (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.font = [UIFont systemFontOfSize:14]; //Change this value to adjust size
cell.textLabel.numberOfLines = 2;
cell.textLabel.text = [NSString stringWithFormat:#"%# %#","Walk to and board at ",[boardDescArray objectAtIndex:indexPath.row]];
cell.textLabel.text = [NSString stringWithFormat:#"%# %#","Alight Destination = ",[alDescArray objectAtIndex:indexPath.row]];
return cell;
}
but when i put two cell.textLabel.text it got an error bad access. what should i do?
Pls help
You only call cell.textLabel.text once, but put a \n in your string where you want a line break.
You also have an error in your stringWithFormat -- either put an # in front of "Walk to and board at" or just remove that first %#, and have :
stringWithFormat:#"Walk to and board at %#",[boardDescArray objectAtIndex:indexPath.row]];
So, to make it 2 lines you want this:
cell.textLabel.text = [NSString stringWithFormat:#"Walk to and board at %#\nAlight Destination = %#",[boardDescArray objectAtIndex:indexPath.row],[alDescArray objectAtIndex:indexPath.row]];

Data not showing in iphone tableView

I am getting data from json. It is working OK but the problem is that data is not showing in tableView.
I have checked the values that I assign to cell using NSLog but cell is not displaying any data.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
int count = [surveyList count];
NSLog(#"These are rows %d",count);
return [surveyList count];
}
- (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];
}
ObjectData *data = [surveyList objectAtIndex:indexPath.row];
NSString *testingClient = data.survey_title;
NSLog(testingClient);
NSString *cellText = testingClient;
return cell;
}
Are you not missing this?
[[cell textLabel] setText:testingClient];
You aren't assigning any text to the cell without it.
You should assign that text to label of cell.
cell.textLabel.text = testingClient;

Add a last cell to UItableview

I have a UITableView whose datasource is a NSMutableArray. The array consists of a set of objects. All the cells are displayed in proper order.
Now I want to know how to display a last cell always with some text alone which is not there in the datasource array.
I hope i am clear enough :)
EDIT :----------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// Return the number of rows in the section.
//+1 to add the last extra row
return [appDelegate.list count]+1;
}
// 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];
}
NSUInteger index=[indexPath row];
if(index ==([appDelegate.list count]+1)) {
cell.textLabel.text = [NSString stringWithFormat:#"extra cell"];
}else{
Item *i = (Item *) [appDelegate.list objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%# (%d %#)",i.iName, i.iQty,i.iUnit];
}
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
but i get the NSMutableArray out of bounds exception.
What could be wrong ?
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [your_array count] + 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
if (row == [your_array count])
{
cell.textLabel.text = [NSString stringWithFormat:#"Some text"];
}
else
{
cell.textLabel.text = your array object text;
}
return cell;
}
Your problem is the value you are checking against index. The list has count objects indexed 0 to count - 1. So you must check against count and not count + 1. As it is now, the request for countth row is entering the else section. There is no object at count in the list array. So you are getting the error. This is the modification.
if(index == [appDelegate.list count] ) {
cell.textLabel.text = [NSString stringWithFormat:#"extra cell"];
}else{
Item *i = (Item *) [appDelegate.list objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%# (%d %#)",i.iName, i.iQty,i.iUnit];
}
in numberOfRowInSection return number of rows = your array count +1 then in Cell for cellForRowAtIndexPath check for indexPath.row if it is equla to your array count +1 then create cell that you want to add at last.

Data is mismanaged when I scroll the UITableView

I am using following code,in cellForRowAtIndexPath:(NSIndexPath *)indexPath method
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
if(currentRow >=[finalArray count]) return cell;
// Configure the cell.
for(int ii=0;ii<[keys count];ii++){
NSString *no22=[dict objectForKey:[keys objectAtIndex:ii]];
no=[NSString stringWithFormat:#"%# %"[keys objectAtIndex:ii],]
}// end of the for loop
if(searching)
cell.text = [copyListOfItems objectAtIndex:indexPath.row];
else {
cell.text=[finalArray objectAtIndex:currentRow];
currentRow=currentRow+1;
}
return cell;
}
I am getting strange behavior when I scroll the table data is completely mismanaged,please any suggestion.
Is because you are reusing cells. You can reuse more type of cells.
And why you are doing if(currentRow >=[finalArray count]) return cell; this?