Load more at the top of uitableview - iphone

I need a load more cell at the top of my table populated with custom cell, the first time that I populate the table is all ok, no overlap and no problem, if I try to load more cell this cell appear but with the wrong height and if I try to scroll the cell change position randomly!
This is my code, where the mistake for you?
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.arrayMessaggi count] + 1;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (indexPath.row == 0) {
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UILabel *labelLoad = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[labelLoad setBackgroundColor:[UIColor clearColor]];
[labelLoad setTextAlignment:NSTextAlignmentCenter];
[labelLoad setText:[NSString stringWithFormat:#"%d - Load more...",indexPath.row]];
[labelLoad setText:#"Load more..."];
[labelLoad setTag:3];
[cell.contentView addSubview:labelLoad];
}
UILabel *labelLoad = (UILabel *)[cell.contentView viewWithTag:3];
[labelLoad setText:[NSString stringWithFormat:#"%d - Load more...",indexPath.row]];
}
else {
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
PFObject *obj = [self.arrayMessaggi objectAtIndex:indexPath.row - 1];
PFUser *user = [obj objectForKey:#"daUtente"];
float height = [self getStringHeight:[obj objectForKey:#"TestoMessaggio"] andFont:[UIFont systemFontOfSize:13]];
UILabel *labelUser = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 280, 20)];
[labelUser setBackgroundColor:[UIColor clearColor]];
[labelUser setText:[user objectForKey:#"username"]];
[labelUser setFont:[UIFont systemFontOfSize:13]];
[labelUser setTag:1];
[cell.contentView addSubview:labelUser];
}
PFObject *obj = [self.arrayMessaggi objectAtIndex:indexPath.row - 1];
PFUser *user = [obj objectForKey:#"daUtente"];
UILabel *labelUser = (UILabel *)[cell.contentView viewWithTag:1];
[labelUser setText:[NSString stringWithFormat:#"%d - %#",indexPath.row,[user objectForKey:#"username"]]];
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
float height = 44;
if (indexPath.row > 0) {
PFObject *obj = [self.arrayMessaggi objectAtIndex:indexPath.row - 1];
NSString *text = [obj objectForKey:#"TestoMessaggio"];
height = 30 + [self getStringHeight:text andFont:[UIFont systemFontOfSize:13]] + 10;
}
return height;
}
- (void) setPullDownToRefresh {
refreshControl = [[UIRefreshControl alloc] init];
[refreshControl addTarget:self action:#selector(aggiornaTabella:) forControlEvents:UIControlEventValueChanged];
[self.messagesTableView addSubview:refreshControl];
}
- (void) aggiornaTabella:(UIRefreshControl *)myRefreshControl {
self.arrayMessaggi = [NSMutableArray arrayWithArray:objects];
self.messagesTableView = [[UITableView alloc] init];
[self.messagesTableView reload data];
}

Related

UITableViewCell row disorder

I posted some question with same source code before. I just found out some other strange thing.The thing is that If I define reuse cell identifier, each row color is weird.
But If I don't use reuse identifier, its working.
Please give me any tips why each row color does not keep the order.
//its working
static NSString *CellIdentifier = #"Cell";
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"] autorelease]
cell.contentView.backgroundColor = (indexPath.row %2) ? [UIColor redColor] : [UIColor yellowColor];
//it does not work.
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; -- does not working.
cell.contentView.backgroundColor = (indexPath.row %2) ? [UIColor redColor] : [UIColor yellowColor];
- (void)viewDidLoad {
[super viewDidLoad];
//Initialize the array.
listOfItems = [[NSMutableArray alloc] init];
//Add items
[listOfItems addObject:#"1"];
[listOfItems addObject:#"2"];
[listOfItems addObject:#"3"];
[listOfItems addObject:#"4"];
[listOfItems addObject:#"5"];
[listOfItems addObject:#"6"];
[listOfItems addObject:#"7"];
[listOfItems addObject:#"8"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [listOfItems count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UILabel *aLabel; UILabel *bLabel; UILabel *v1Label; UILabel *v2Label;; UIView *v1; UIView *v2;
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSLog(#" cell null");
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.contentView.backgroundColor = (indexPath.row %2) ? [UIColor redColor] : [UIColor yellowColor];
aLabel = [[[UILabel alloc] initWithFrame:CGRectMake(9.0, 8.0, 50.0, 20.0)] autorelease];
aLabel.tag = 1;
aLabel.font = [UIFont systemFontOfSize:30];
[cell.contentView addSubview:aLabel];
v1 = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 116)];
v1.backgroundColor = [UIColor whiteColor];
v1.tag = 10;
v1.hidden = YES;
[cell.contentView addSubview:v1];
[v1 release];
UILabel *a = [[[UILabel alloc] initWithFrame:CGRectMake(0, 10, 100, 100)] autorelease];
a.text = #"v1.test1";
[v1 addSubview:a];
UILabel *b = [[[UILabel alloc] initWithFrame:CGRectMake(0, 40, 100, 100)] autorelease];
b.text = #"v1.test2";
[v1 addSubview:b];
v2 = [[UIView alloc] initWithFrame:CGRectMake(0, 44, 320, 100)];
v2.backgroundColor = [UIColor blueColor];
v2.tag = 11;
v2.hidden = YES;
[cell.contentView addSubview:v2];
[v2 release];
UILabel *c = [[[UILabel alloc] initWithFrame:CGRectMake(0, 10, 100, 100)] autorelease];
c.text = #"v2.test1";
[v2 addSubview:c];
UILabel *d = [[[UILabel alloc] initWithFrame:CGRectMake(0, 40, 100, 100)] autorelease];
d.text = #"v2.test2";
[v2 addSubview:d];
}
else {
aLabel = (UILabel *)[cell.contentView viewWithTag:1];
//
v1 = (UIView *) [cell.contentView viewWithTag:10];
v2 = (UIView *) [cell.contentView viewWithTag:11];
}
aLabel.text = [listOfItems objectAtIndex:indexPath.row];
if (SelectedIndexPath == indexPath.row)
{
if ([aLabel.text intValue] % 2) {
v1.hidden = NO;
v2.hidden = YES;
}
else {
v1.hidden = YES;
v2.hidden = NO;
}
}
else {
v1.hidden = YES;
v2.hidden = YES;
}
return cell;
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (SelectedIndexPath == indexPath.row)
{
return 162.0;
}
else {
return 46.0;
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (SelectedIndexPath == -1)
{
OldSelectedIndexPath = indexPath.row;
SelectedIndexPath = indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:NO];
}
else
{
if (SelectedIndexPath == indexPath.row)
{
OldSelectedIndexPath = SelectedIndexPath;
SelectedIndexPath = -1;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:NO];
}
else
{
SelectedIndexPath = indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:[NSIndexPath indexPathForRow:OldSelectedIndexPath inSection:indexPath.section], indexPath, nil] withRowAnimation:NO];
OldSelectedIndexPath = SelectedIndexPath;
}
}
}
Move the coloring code outside of the if (cell == nil) { block. Just because a cell was created for an even-numbered index doesn't mean it will only be reused for even ones.
Here's a simple example of code for coloring cells that are being reused:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
- (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.textLabel.text = #"Anything";
cell.contentView.backgroundColor = (indexPath.row %2) ? [UIColor redColor] : [UIColor yellowColor];
return cell;
}

iphone:UItableviewcell duplicates when using table reload

My tableview gets duplicated when I use [tableview reloadData].I used this to refresh the tableview when I move back.How could I overcome the issue?
This is my code.
#define kMyTag 1
#define despTag 2
#define subTag 3
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.tableview reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
AsyncImageView *asyncImageView = nil;
UILabel *label = nil;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] ;
UILabel *mainLabel=[[UILabel alloc]init];
mainLabel.tag = kMyTag; // define kMyTag in your header file using #define
mainLabel.frame=CGRectMake(95, 0, 170, 30);
mainLabel.font = [UIFont boldSystemFontOfSize:14];
mainLabel.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:mainLabel];
UILabel *despLabel=[[UILabel alloc]init];
despLabel.tag=despTag;
despLabel.frame=CGRectMake(95, 25, 190, 20);
despLabel.font= [UIFont systemFontOfSize:12];
despLabel.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:despLabel];
} else {
label = (UILabel *) [cell.contentView viewWithTag:LABEL_TAG];
}
switch([indexPath section]){
case 0:
{
NSString *urlString = [img objectAtIndex:indexPath.row];
urlString=[urlString stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
tname=[mname objectAtIndex:indexPath.row];
tprice=[mprice objectAtIndex:indexPath.row];
tquan=[mquan objectAtIndex:indexPath.row];
tspice=[mspice objectAtIndex:indexPath.row];
UILabel *mainLabel=(UILabel *)[cell.contentView viewWithTag: kMyTag];
mainLabel.text = [NSString stringWithFormat:#"%#",tname];
UILabel *despLabel=(UILabel *)[cell.contentView viewWithTag: despTag];
despLabel.text = [NSString stringWithFormat:#"Qty :%# Spice: %#",tquan,tspice];
UILabel *subLabel=(UILabel *)[cell.contentView viewWithTag: subTag];
float pric=[tprice floatValue];
subLabel.text = [NSString stringWithFormat:#"Price :%# %.2f",currencyType, pric];
}
}
}
Overcomes the issue by removing all the objects from array,then reselects the DB table & reloads uitableview.
it happens because you are not passing any value in dequeueReusableCellWithIdentifier
pass a string value and then use it in reuseIdentifier also

problem with table view grouped table view

tableview problem: i am using 3 uilable for displaying productname, some description and image. all data displayed but when scrolling the table the labels are filled with another text with the actual text.. how can we handle 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:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(2, 2, 41, 41)];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
NSDictionary *aDict1 = [[NSDictionary alloc]init];
aDict1 = [tableData objectAtIndex:indexPath.row];
NSString *prdStus = [aDict1 objectForKey:#"ProductStatus"];
NSLog(#"product status is %# ",prdStus);
if ([prdStus isEqualToString:#"Orange"]) {
[imageview setImage:[UIImage imageNamed:#"Yellow.png"]];
}
if ([prdStus isEqualToString:#"Green"]) {
[imageview setImage:[UIImage imageNamed:#"Green.png"]];
}
if ([prdStus isEqualToString:#"Red"]) {
[imageview setImage:[UIImage imageNamed:#"Red.png"]];
}
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(46, 0, 220, 12) ];
label2.font = [UIFont systemFontOfSize:12];
label2.text = #"";
if (indexPath.row <[tableData count]) {
label2.text = [aDict1 objectForKey:#"ProductName"];
}
[cell addSubview:label2];
[cell addSubview:imageview];
label2.backgroundColor =[UIColor clearColor];
UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(46, 13, 220, 30) ];
label3.font = [UIFont systemFontOfSize:10];
label3.text = #"";
label3.text = [aDict1 objectForKey:#"ProductDescription"];
[cell addSubview:label3];
return cell;
}
please tell me how to avoid this..
Grouped Table view.
Here is also i am facing same problem.
I am using 4 section
1st and 3rd sections 1 row each,
2nd sec 3 rows,
4th sec 5
when configure the text the first section label displayed on 4th and 3rd section data also displayed on 4th section.
Code
- (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.userInteractionEnabled =NO;
if (indexPath.section == 0)
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(12, 2, 294, 40) ];
label.backgroundColor = [UIColor clearColor];
NSDictionary *aDict1 = [[NSDictionary alloc]init];
aDict1 = [detailsArray objectAtIndex:0];
label.text=#"";
label.text =[aDict1 objectForKey:#"ProductName"];
label.numberOfLines = 2;
label.lineBreakMode = UILineBreakModeWordWrap;
[cell addSubview:label];
[label release];
// [cell addSubview:imgview1];
// [imgview1 release];
}
if (indexPath.section ==1 ) {
if (indexPath.row == 0)
{
imgview = [[UIImageView alloc]initWithFrame:CGRectMake(255,2 , 46, 46)];
[cell addSubview:imgview];
[imgview release];
cell.textLabel.text = #"Actual Halal Rating";
NSDictionary *aDict1 = [[NSDictionary alloc]init];
aDict1 = [detailsArray objectAtIndex:0];
NSString *statStr=[[NSString alloc] init];
statStr = [aDict1 objectForKey:#"ProductHalalStatus"];
NSLog(#"status is %#",statStr);
imgview1 = [[UIImageView alloc]initWithFrame:CGRectMake(255,2 , 20, 20)];
if ([statStr isEqualToString:#"Red"]) {
[imgview1 setImage:[UIImage imageNamed:#"Red.png"]];
}
if ([statStr isEqualToString:#"Orange"] ) {
[imgview1 setImage:[UIImage imageNamed:#"Yellow.png"]];
}
if ([statStr isEqualToString:#"Green"]) {
[imgview1 setImage:[UIImage imageNamed:#"Green.png"]];
}
[cell addSubview:imgview1];
[imgview1 release];
}
if (indexPath.row == 1) {
cell.textLabel.text = #"Halal (Permisible)";
[imgview setImage:[UIImage imageNamed:#"Green.png"]];
}
if (indexPath.row == 2) {
cell.textLabel.text = #"Masbooh (Doubtful)";
[imgview setImage:[UIImage imageNamed:#"Yellow.png"]];
}
if (indexPath.row == 3) {
cell.textLabel.text = #"Haram (Not Permisible)";
[imgview setImage:[UIImage imageNamed:#"Red.png"]];
}
}
if (indexPath.section == 2) {
NSDictionary *aDict2 = [[NSDictionary alloc]init];
aDict2 = [detailsArray objectAtIndex:0];
// NSArray *ingrArr =[aDict2 objectForKey:#"IngredientInfo1"];
textview1 =[[UITextView alloc]initWithFrame:CGRectMake(12, 2, 294, 96)];
//textview1.text = ingrStr;
textview1.editable =NO;
[textview1 setFont:[UIFont systemFontOfSize:15]];
[cell addSubview:textview1];
[textview1 release];
}
if (indexPath.section == 3) {
}
return cell;
}
Remove the line, from you code ....
if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
and add the following line
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]

read text of selected cell

I have 2 columns in my table:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSUInteger const kLeftLabel =100;
static NSUInteger const kRightLabel=101;
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
CGRect leftF = CGRectMake(10, 5, 100, 30);
CGRect rightF = CGRectMake(120, 5, 100, 30);
left = [[[UILabel alloc] initWithFrame:leftF] autorelease];
left.tag = kLeftLabel; // assumming #define kLeftLabel 100
[cell.contentView addSubview:left];
right = [[[UILabel alloc] initWithFrame:rightF] autorelease];
right.tag = kRightLabel;
[cell.contentView addSubview:right];
}
else{
left= (UILabel*)[cell.contentView viewWithTag:kLeftLabel];
right = (UILabel*)[cell.contentView viewWithTag:kRightLabel];
}
profileDB *Obj = [appDelegate.profileArray objectAtIndex:indexPath.row];
left.text = Obj.profileName;
right.text = Obj.id;
return cell;
}
I want to read the right column's text only for a selected cell.
Is this possible?
First, You have to find out the selected cell.
- (void) tableView:(UITableView*)tableview didSelectRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell *cell = [tableview cellForRowAtIndexPath:indexPath];
for(UILabel *lbl in [cell.contentView subviews])
{
if(([lbl isKindOfClass:[UILabel class]]) && ([lbl tag] == 101))
{
NSString *str = lbl.textLabel.text;
}
}
}
If you have added subviews on cell, the you can specific label's text by using their tag

Problem adding UITableViewCell manually

I am trying to add a UITableViewCell at the very bottom of my UITableView programatically. I am getting an index out of bounds error, which I am not sure how to resolve:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [items count]+1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
if (indexPath.row == [items count] + 1) {
UITableViewCell* cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MoreCell"] autorelease];
cell.textLabel.text = #"More...";
return cell;
}
else {
STUser *mySTUser;
mySTUser = [items objectAtIndex:indexPath.row]; //!!INDEX OUT OF BOUNDS HERE!!
AsyncImageView* asyncImage = nil;
UILabel* loginLabel = nil;
UILabel* fullNameLabel = nil;
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
CGRect frame = CGRectMake(0, 0, 44, 44);
CGRect loginLabelFrame = CGRectMake(60, 0, 200, 10);
CGRect fullNameLabelFrame = CGRectMake(60, 20, 200, 10);
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:nil] autorelease];
asyncImage = [[[AsyncImageView alloc]initWithFrame:frame] autorelease];
[cell.contentView addSubview:asyncImage];
loginLabel = [[[UILabel alloc] initWithFrame:loginLabelFrame] autorelease];
[cell.contentView addSubview:loginLabel];
fullNameLabel = [[[UILabel alloc] initWithFrame:fullNameLabelFrame] autorelease];
[cell.contentView addSubview:fullNameLabel];
asyncImage.tag = IMAGE_TAG;
loginLabel.tag = LOGIN_TAG;
fullNameLabel.tag = FULL_NAME_TAG;
}
else {
asyncImage = (AsyncImageView *) [cell.contentView viewWithTag:IMAGE_TAG];
loginLabel = (UILabel *) [cell.contentView viewWithTag:LOGIN_TAG];
fullNameLabel = (UILabel *) [cell.contentView viewWithTag:FULL_NAME_TAG];
}
// Configure the cell...
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
CGRect frame = CGRectMake(0, 0, 44, 44);
asyncImage = [[[AsyncImageView alloc]initWithFrame:frame] autorelease];
NSURL* url = [NSURL URLWithString:mySTUser.avatar_url_large];
[asyncImage loadImageFromURL:url];
asyncImage.tag = IMAGE_TAG;
[cell.contentView addSubview:asyncImage];
CALayer * l = [asyncImage layer];
[l setMasksToBounds:YES];
[l setCornerRadius:5.0];
CGRect loginLabelFrame = CGRectMake(60, 0, 200, 20);
loginLabel = [[[UILabel alloc] initWithFrame:loginLabelFrame] autorelease];
loginLabel.text = [NSString stringWithFormat:#"%#",mySTUser.login];
loginLabel.tag = LOGIN_TAG;
[cell.contentView addSubview:loginLabel];
CGRect fullNameLabelFrame = CGRectMake(60, 20, 200, 20);
fullNameLabel = [[[UILabel alloc] initWithFrame:fullNameLabelFrame] autorelease];
fullNameLabel.text = [NSString stringWithFormat:#"%# %#",mySTUser.first_name, mySTUser.last_name];
fullNameLabel.tag = FULL_NAME_TAG;
fullNameLabel.font = [UIFont fontWithName:#"Helvetica" size:(12.0)];
[cell.contentView addSubview:fullNameLabel];
return cell;
}
}
If the count is items + 1 I want to return the More Cell, if not, then I want it to create all of the other cells. Not sure why I get the index out of bounds since the code should never reach that point.
indexPath.row is zero based.
Change:
if (indexPath.row == [items count] + 1) {
To:
if (indexPath.row == [items count]) {
Arrays in Objective-C and almost all other programming languages are zero-based. That means the first object has index 0, and the last object has index (length - 1).
Therefore, the condition
if (indexPath.row == [items count] + 1) {
should be changed to
if (indexPath.row == [items count]) {