UITableViewCell issue indexPath.row not displaying correctly - objective-c

I have set up a UITableView which has 3 sections in, with 3 rows in each. I ahve used the following code to specify that I want the objects from certain arrays to appear as the row content;
recipeData = [[NSArray alloc] initWithObjects:#"Chicken & Veg Rissoto",#"Super Healthy Pizza",#"Salmon Burgers",nil];
dessertsData = [[NSArray alloc] initWithObjects:#"Raspberry Parfaits",#"ChocNana YumYum",#"Grilled Choc Sandwich",nil];
beveragesData = [[NSArray alloc] initWithObjects:#"Berry Smoothie",#"Banana Berry Smoothie",#"Cocoa Soy Smoothie",nil];
[self setTitle:#"Recipe Table"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.recipeData count];
return [self.dessertsData count];
return [self.beveragesData count];
This was all working fine, until this evening when I added in images to the left of the text in the row. The images display perfectly, but for some reason the rows are all populated with the objects from beveragesData and it totally disregards the other two sets of objects. The code is below that I have used. I am hoping it is something as simple as just writing the if statements incorrectly.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SimpleTableIdentifier] autorelease];
}
if(indexPath.section == 0)
if(indexPath.row == 0)
[cell.imageView setImage:[UIImage imageNamed:#"Main0.png"]];
[cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];
if(indexPath.section == 0)
if(indexPath.row == 1)
[cell.imageView setImage:[UIImage imageNamed:#"Main1.png"]];
[cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];
if(indexPath.section == 0)
if(indexPath.row == 2)
[cell.imageView setImage:[UIImage imageNamed:#"Main2.png"]];
[cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];
if(indexPath.section == 1)
if(indexPath.row == 0)
[cell.imageView setImage:[UIImage imageNamed:#"Dessert0.png"]];
[cell.textLabel setText:[dessertsData objectAtIndex:indexPath.row]];
if(indexPath.section == 1)
if(indexPath.row == 1)
[cell.imageView setImage:[UIImage imageNamed:#"Dessert1.png"]];
[cell.textLabel setText:[dessertsData objectAtIndex:indexPath.row]];
if(indexPath.section == 1)
if(indexPath.row == 2)
[cell.imageView setImage:[UIImage imageNamed:#"Dessert2.png"]];
[cell.textLabel setText:[dessertsData objectAtIndex:indexPath.row]];
if (indexPath.section == 2)
if(indexPath.row == 0)
[cell.imageView setImage:[UIImage imageNamed:#"Drink0.png"]];
[cell.textLabel setText:[beveragesData objectAtIndex:indexPath.row]];
if(indexPath.section == 2)
if(indexPath.row == 1)
[cell.imageView setImage:[UIImage imageNamed:#"Drink1.png"]];
[cell.textLabel setText:[beveragesData objectAtIndex:indexPath.row]];
if(indexPath.section == 2)
if(indexPath.row == 2)
[cell.imageView setImage:[UIImage imageNamed:#"Drink2.png"]];
[cell.textLabel setText:[beveragesData objectAtIndex:indexPath.row]];
return cell;
So at present, when that runs all 9 rows of the UITableView are populated with the objects from beveragesData. Any help would be greatly appreciated.
Thanks

Objective-c structure isn't based on indentation
This:
if(indexPath.section == 0)
if(indexPath.row == 0)
[cell.imageView setImage:[UIImage imageNamed:#"Main0.png"]];
[cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];
Should be:
if(indexPath.section == 0)
if(indexPath.row == 0) {
[cell.imageView setImage:[UIImage imageNamed:#"Main0.png"]];
[cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];
}
It's like C/C++/Java -- the if applies to the next statement. If you need more than one, you need to enclose the statements in curly braces. You could also put in curlies all of the time to not worry about it.
Also, this code
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.recipeData count];
return [self.dessertsData count];
return [self.beveragesData count];
}
Doesn't do what you think it does. The first return is always run and the second two lines are ignored (should have gotten a warning about that).

Your numberOfRowsInSection: section is not correct either; should be:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ( section == 0 )
return [self.recipeData count];
if ( section == 1 )
return [self.dessertsData count];
if ( section == 3 )
return [self.beveragesData count];
}

You should also use a switch statement:
switch ( indexPath.section )
{
case 0:
{
if(indexPath.row == 0) {
[cell.imageView setImage:[UIImage imageNamed:#"Main0.png"]];
[cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];
}
if(indexPath.row == 1) {
[cell.imageView setImage:[UIImage imageNamed:#"Main1.png"]];
[cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];
}
if(indexPath.row == 2) {
[cell.imageView setImage:[UIImage imageNamed:#"Main2.png"]];
[cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];
}
}
}
/* etc. */

Related

Add right detail to table view cell

I cannot seem to work out how to add text in a different colour to the right side of my table view cell. Here is the code I have added.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *informationTableIdentifier = #"InformationCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:informationTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:informationTableIdentifier];
}
[cell.textLabel setFont:[UIFont systemFontOfSize:15]];
[cell.textLabel setTextColor:[UIColor blackColor]];
if (indexPath.section == 0) {
cell.textLabel.text = [infoCellFirstSection objectAtIndex:indexPath.row];
}
if (indexPath.section == 1) {
if (indexPath.row == 3 || indexPath.row == 3) {
cell.detailTextLabel.text = #"#SykesHarvey";
[cell.detailTextLabel setTextColor:[UIColor colorWithRed:0.48 green:0.93 blue:0.25 alpha:1.0]];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:informationTableIdentifier];
}
cell.textLabel.text = [infoCellSecondSection objectAtIndex:indexPath.row];
}
if (indexPath.section == 2) {
if (indexPath.row == 2 || indexPath.row == 3) {
[cell.textLabel setFont:[UIFont systemFontOfSize:14]];
[cell.textLabel setTextColor:[UIColor grayColor]];
cell.textLabel.numberOfLines = 0;
cell.backgroundColor = [UIColor clearColor];
[cell setAccessoryType:UITableViewCellAccessoryNone];
}
cell.textLabel.text = [infoCellThirdSection objectAtIndex:indexPath.row];
}
if (indexPath.section == 3) {
cell.textLabel.text = [infoCellFourthSection objectAtIndex:indexPath.row];
}
if (indexPath.section == 4) {
cell.textLabel.text = [infoCellFifthSection objectAtIndex:indexPath.row];
}
return cell;
}
All this is presenting is but I want it to say #SykesHarvey on the right side of the Developer cell. Please can someone help?
You are reseting the cell detailTextLabel by reinitialise it in next line.
I have optimise your code a little. Try this :
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *informationTableIdentifier = #"InformationCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:informationTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:informationTableIdentifier];
}
[cell.textLabel setFont:[UIFont systemFontOfSize:15]];
[cell.textLabel setTextColor:[UIColor blackColor]];
switch (indexPath.section) {
case 0:
cell.textLabel.text = [infoCellFirstSection objectAtIndex:indexPath.row];
break;
case 1:
if (indexPath.row == 3 || indexPath.row == 3) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:informationTableIdentifier];
[cell.textLabel setFont:[UIFont systemFontOfSize:15]];
[cell.textLabel setTextColor:[UIColor blackColor]];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
cell.detailTextLabel.text = #"#SykesHarvey";
[cell.detailTextLabel setTextColor:[UIColor colorWithRed:0.48 green:0.93 blue:0.25 alpha:1.0]];
}
cell.textLabel.text = [infoCellSecondSection objectAtIndex:indexPath.row];
break;
case 2:
if (indexPath.row == 2 || indexPath.row == 3) {
[cell.textLabel setFont:[UIFont systemFontOfSize:14]];
[cell setAccessoryType:UITableViewCellAccessoryNone];
[cell.textLabel setTextColor:[UIColor grayColor]];
cell.textLabel.numberOfLines = 0;
cell.backgroundColor = [UIColor clearColor];
}
cell.textLabel.text = [infoCellThirdSection objectAtIndex:indexPath.row];
break;
case 3:
cell.textLabel.text = [infoCellFourthSection objectAtIndex:indexPath.row];
break;
case 4:
cell.textLabel.text = [infoCellFifthSection objectAtIndex:indexPath.row];
break;
default:
break;
}
return cell;
}
Try this:
cell.detailTextLabel.textAlignment = NSTextAlignmentRight;

overriding cells in UITableView while scrolling

I know this is repeated question but i am not getting solution on my issue..I have designed UITableView programmatically but it's working fine till 4 sections or 9 rows but when I am trying to add any extra cell it's overriding first cell on last cell and vice versa.Please solve my issue.Thanks in advance.
my code is -
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 6;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 0)
return 1;
else if (section == 1)
return 1;
else if (section == 2)
return 3;
else if (section == 3)
return 2;
else if (section == 4)
return 2;
else if (section == 5)
return 3;
else
return 0;
}
-(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];
}
if ((indexPath.section == 0) && (indexPath.row ==0)) {
cell.textLabel.text = #"";
//Get it from previous view
chooseEventLbl.text = #"Choose an event";
//chooseEventLbl.textColor = [UIColor darkGrayColor];
[cell addSubview:chooseEventLbl];
//cell.textLabel.text = #"Choose an event";
UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 20)];
[image setImage:[UIImage imageNamed:#"arrow_list.png"]];
cell.accessoryView = [[UIImageView alloc]initWithFrame:CGRectMake(260.0, 7.0, 10, 20)];
[cell.accessoryView addSubview:image];
}
else if ((indexPath.section == 1) && (indexPath.row == 0))
{
cell.textLabel.text = #"";
[cell addSubview:squareNameTxt];
}
else if ((indexPath.section == 2) && (indexPath.row == 0))
{
cell.textLabel.text = #"Public";
}
else if ((indexPath.section == 2) && (indexPath.row == 1))
{
cell.textLabel.text = #"Private";
}
else if ((indexPath.row == 2) && (indexPath.row == 2) && (passwordFlag == TRUE))
{
cell.textLabel.text = #"";
[cell addSubview:passwordTxt];
// passwordTxt.enabled = NO;
}
else if ((indexPath.section == 3) && (indexPath.row == 0))
{
cell.textLabel.text = #"Allow multiple Squares";
[aSwitch addTarget:self action:#selector(switchChanged:) forControlEvents:UIControlEventValueChanged];
aSwitch.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:aSwitch];
}
else if ((indexPath.section == 3) && (indexPath.row == 1)) {
cell.textLabel.text = #"";
[cell addSubview:multiSquareListBtn];
}
else if ((indexPath.section == 4) && (indexPath.row == 0))
{
cell.textLabel.text = #"Prizes";
}
else if ((indexPath.section == 4) && (indexPath.row == 1))
{
cell.textLabel.text = #"";
[cell addSubview: firstQuarterTxt];
[cell addSubview:halftimeTxt];
[cell addSubview:thirdQuarterTxt];
[cell addSubview:finalTxt];
}
else if ((indexPath.section == 5) && (indexPath.row ==0))
{
cell.textLabel.text= #"";
currentLocationLbl.text = #"Choose Current Location";
[cell addSubview:currentLocationLbl];
}
else if ((indexPath.section == 5) && (indexPath.row == 1))
{
cell.textLabel.text = #"";
mapLocationLbl.text = #"Choose location from Map";
[cell addSubview:mapLocationLbl];
}
else if ((indexPath.section == 5) && (indexPath.row == 2))
{
cell.textLabel.text = #"";
radiusLocationLbl.text = #"Location Radius";
[cell addSubview:radiusLocationLbl];
}
return cell;
}

UITableView cell labels moving around when scrolling

I created a uitableview and when i launch it, it looks as it is supposed to, but when I scroll, it puts text in all the other sections that I don't specify, can someone please help.
The first image attached is how it should look. The second it what it does when I scroll.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0 ){
return 1;}
else if (section == 1){
return [cellLabels count];
}else if (section == 2){
return 1;
}else{
return 0;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
}
tableView.showsVerticalScrollIndicator = NO;
// cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
if( indexPath.section == 0 )
{
}
else if (indexPath.section == 1)
{
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor blackColor];
// headerLabel.font = [UIFont SystemFontOfSize:16];
[cell.textLabel setFont:[UIFont fontWithName:#"Arial" size:14]];
cell.textLabel.text = [cellLabels objectAtIndex:indexPath.row];
}
return cell;
}
your problem is pretty simple ,
since table view reuses allocated cells,
when it comes to first time to first section your displaying nothing , in second section displaying your custom texts
when it scrolls down and come back it text will appear in first section because when it reaches
if( indexPath.section == 0 )
{
}
it wont do anything so
make it
if( indexPath.section == 0 )
{
cell.textLabel.text = #"";
}
else if( indexPath.section == 2 )
{
cell.textLabel.text = #"";
}
or
if( indexPath.section == 0 )
{
cell.textLabel.text = nil;
}
else if( indexPath.section == 2 )
{
cell.textLabel.text = nil;
}
other FOR SECTION 1 is correct

Displaying two sections of specific size in a table view

I am trying to fill a table view with an array, but I want two sections, and in the first section I want to display the imformation from indexpath.row == 0 to indexpath.row == 4 and in the second section the information from indexpath.row == 5 to indexpath.row == 9. How can I do that? I have this code for the second section:
if (indexPath.section == 1){
static NSString *CellIdentifier = #"LazyTableCell";
static NSString *PlaceholderCellIdentifier = #"PlaceholderCell";
int nodeCount = [self.entries count];
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier] autorelease];
if (nodeCount > 0)
{
if (indexPath.row > 4) {
AppRecord *appRecord = [self.entries objectAtIndex:indexPath.row];
cell.textLabel.text = appRecord.artist;
cell.detailTextLabel.text = appRecord.appName;
if (!appRecord.appIcon)
{
if (self.tableView.dragging == NO && self.tableView.decelerating == NO)
{
[self startIconDownload:appRecord forIndexPath:indexPath];
}
cell.imageView.image = [UIImage imageNamed:#"Placeholder.png"];
}
else
{
UIImage *image = appRecord.appIcon;
CGSize itemSize = CGSizeMake(83.0, 48.0);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0.0, 0.0, 83.0, 48.0);
[image drawInRect:imageRect];
appRecord.appIcon = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.imageView.image = appRecord.appIcon;
}
}
return cell;
}
}
set the number of section in this function
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 2;
}
Perhaps you also want to implement
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return #"section title";
}
set the number of lines in the following function
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5; // since you have 0-4 and 5-9 for the two sections
}
after seeing in your code [self.entries objectAtIndex:indexPath.row];
I assumed self.entries is the one array that you have been talking about.
You can initialize the UITableViewCell property in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
by using a combination of section and row to get the index of your self.entries, probably something like
AppRecord *appRecord = [self.entries objectAtIndex:indexPath.row+5*indexPath.section];
I hope i got your question right.

sectioned UITableView

I am making an application for the iPhone, in which I am creating UITableView with 2 sections. I have created sections parts of UITableView, but the problem is that I need different images in every cell of each section. Does anyone has any solution for this
Yes, that's very easy. Have you this in your datasource yet:?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Add the following code to it:
NSString *CellIdentifier = [NSString stringWithFormat:#"cell_%i",indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
if (indexPath.section == 0 && indexPath.row == 0){
[cell.imageView setImage:[UIImage imageNamed:#"justanimage.png"]];
}else if (indexPath.section == 0 && indexPath.row == 1){
[cell.imageView setImage:[UIImage imageNamed:#"justanimage1.png"]];
}else if (indexPath.section == 1 && indexPath.row == 0){
[cell.imageView setImage:[UIImage imageNamed:#"justanimage2.png"]];
}else if (indexPath.section == 1 && indexPath.row == 1){
[cell.imageView setImage:[UIImage imageNamed:#"justanimage3.png"]];
}
}
You can also save your data in a NSMutableArray, but that's a little bit more complicated.