UITableView crashes on scrolling - iphone

UITableView is crashing on scrolling . Here is the sample code..
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(#"Total Count: %d",count);
return [ObjectArray count] ;
}
- (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.numberOfLines = 0;
cell.textLabel.font = [UIFont fontWithName:#"Arial" size:12.0];
cell.detailTextLabel.font = [UIFont fontWithName:#"Arial" size:10.0];
[cell.textLabel sizeToFit];
[cell setAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
}
// Configure the cell.
NSLog(#"Row Number %d", indexPath.row);
cell.textLabel.text = [ObjectArray name];
return cell;
}
In console I can see Total count = 22.
Row Number 0
Row NUmber 1
.
.
Row Number 21 (This row number will pull the last object from ObjectArray)
Now if I try to scroll it crashes...why? Can anyone help me with this ...

I don't really understand this line of code
cell.textLabel.text = [ObjectArray name];
If ObjectArray is a NSArray instance, then it would not respond to that selector. And anyway you probably want to return smth like
cell.textLabel.text = [ObjectArray objectAtIndex: [indexPath row]];

Related

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]];

Add One More Cell to tableView

My issue is similar what this guy had:
Add a last cell to UItableview
In fact I'm using the same method which got selected in that question, here's the code snippet:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [array count] + 1;
}
- (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];
// cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
}
// Configure the cell.
NSUInteger rowN = [indexPath row];
if (rowN == [array count]) {
cell.textLabel.text = [NSString stringWithFormat:#"Some Text"];
} else {
TFHppleElement* pCell = [array objectAtIndex:indexPath.row];
cell.textLabel.text = [NSString stringWithFormat:#"%#", [pCell content]];
}
return cell;
}
Now, I'm getting exception error: *** -[__NSArrayM objectAtIndex:]: index 6 beyond bounds [0 .. 5].
What could be the issue? From the error, it looks like that [array objectAtIndex:indexPath.row] is trying to access 6th index of the array which doesn't exist but why it is trying to access 6th index when I have the code in else condition?
Change this row:
NSUInteger rowN = [indexPath row];
to:
NSUInteger rowN = indexPath.row;
Or use just the indexPath.row instead of the rowN in the if condition

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.

Add images to table cells in sections

i want to display an image besides the text in table cell. i have 2 sections, General and Assignments. Now i only want to display image beside Assignments and no image besides General section. the below code displays images for Assignments section and then again repeats the images for General section. Can anybody help me how to accompalish this.
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
staffImages = [[NSMutableArray alloc] init];
NSArray *arrTemp1 = [[NSArray alloc] initWithObjects:#"TRANSPORTATION",#"ROOMS",#"ACTIVITIES",nil];
NSArray *arrTemp2 = [[NSArray alloc] initWithObjects:#"CHAT",#"SEARCH",#"CALL",#"MORE",nil];
NSDictionary *temp =[[NSDictionary alloc] initWithObjectsAndKeys:arrTemp1,#"Assignments",arrTemp2,
#"General",nil];
//Add item images
[staffImages addObject:#"transportation.png"];
[staffImages addObject:#"hotel.png"];
[staffImages addObject:#"activities.png"];
//Set the title
self.navigationItem.title = #"STAFF ASSIGNMENTS";
self.tableContents=temp;
[temp release];
self.sortedKeys =[self.tableContents allKeys];
[arrTemp1 release];
[arrTemp2 release];
[super viewDidLoad];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if(cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
cell.imageView.image = [UIImage imageNamed:[staffImages objectAtIndex:indexPath.row]];
return cell;
}
NSMutableData *data=[NSMutableData dataWithContentsOfURL:[NSURL URLWithString:[NSMutableString stringWithFormat:#"%#",[[dataArray objectAtIndex:indexPath.row]objectForKey:#"LINK"]]]];
//NSLog(#"%#",data);
UIImage *img=[UIImage imageWithData:data];
cell.imageView.image=img;
this is best way
Assuming the general section is section 0 (i.e. first section)
if (indexPath.section == 1) {
cell.imageView.image = [UIImage imageNamed:[staffImages objectAtIndex:indexPath.row]];
}
You need to check the section of the indexPath. You probably will also need to use the section check to make sure that you set the text of the cell to the right value.
//You do not need to actually define this value
//this is just for naming purposes
#define SECTION_ASSIGNMENTS 0
...
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
NSArray *listData =[self.tableContents objectForKey:[self.sortedKeys objectAtIndex:[indexPath section]]];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if(cell == nil) {
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
if(indexPath.section == SECTION_ASSIGNMENTS)
{
cell.imageView.image = [UIImage imageNamed:[staffImages objectAtIndex:indexPath.row]];
}
return cell;
}

Load Distinct Images from array into UITabeView

I am trying to load three different images from an array into corresponding cells in a UITable. So far I have the following code which builds fine crashes when t is run. I fanyone can helps me out I would be very grateful.
- (void)viewDidLoad {
NSArray *array = [[NSArray alloc] initWithObjects:#"Icon Nightclub", #"Smyhts Bar",
#"Synotts",nil];
self.listData = array;
NSArray *picArray = [[NSArray alloc] initWithObjects:#"ArrowLeftDefault.png", #"ArrowRightDefault.png",
#"events.png",nil];
self.picData = picArray;
[array release];
[picArray release];
[super viewDidLoad];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [listData count];
}
- (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];
cell.textColor = [UIColor grayColor];
cell.textLabel.text = [listData objectAtIndex:row];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
cell.imageView.image = [picData objectAtIndex:indexPath.row];
return cell;
}
change:
cell.imageView.image = [picData objectAtIndex:indexPath.row];
for something like this:
cell.imageView.image = [UIImage imageNamed:[picData objectAtIndex:indexPath.row]];
Explanation:
Since picData NSArray contains NSStrings, you were passing a NSString object to cell.imageView.image when it expects an UIImage object actually.
That is why now it creates an image and passes it. (imageNamed: method cashes the image)