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]
Related
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];
}
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;
}
in my app i have an UITableView with 120 rows and every row has 1 UItextfeilds and 1 Buttons as show in the code bellow :
-(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];
}
else
{
}
UITextField *NameTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 10, 230, 28)];
NameTextField.borderStyle = UITextBorderStyleRoundedRect;
NameTextField.delegate = self;
NameTextField.textColor = UIColorFromRGB(0x2A1807);
NameTextField.font = [UIFont fontWithName:#"Helvetica" size:(17.0)];
NameTextField.font = [UIFont boldSystemFontOfSize:20];
NameTextField.tag = [indexPath row ];
NSString *temp = [self.sectionNames objectAtIndex:[indexPath section]];
NameTextField.text = [[self.myDataArray objectForKey:temp] objectAtIndex:[indexPath row]];
[cell.contentView addSubview:NameTextField];
[NameTextField release];
UIButton * aBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *wijzigImage = [UIImage imageNamed:#"btn_delete.png"];
aBtn.frame = CGRectMake(240, 10, 28, 26);
[aBtn setImage:wijzigImage forState:UIControlStateNormal];
[aBtn addTarget:self action:#selector(deleteCustomCellWithUIButton:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:aBtn];
return cell;
}
i have noticed that the Scrolling is slow and It doesn't go fluently.
any idea ?
Thanks
That's because you create textfields and buttons every time, add it inside if (cell == nil) {...}. The only thing that should be left outside that if is textField text setting.
Btw, your textfield leaks.
I found the solution :
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
else
{
UITextField *oldTextField = (UITextField *)[cell.contentView viewWithTag:999];
[oldTextField removeFromSuperview];
UIButton *oldBtn = (UIButton *)[cell.contentView viewWithTag:888];
[oldBtn removeFromSuperview];
}
NameTextField.tag = 999;
aBtn.tag = 88;
I have a big problem with an UITableView, I want to use a label inside the cell, so I use this method for do it
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"ItemCell";
// If the indexPath is less than the numberOfItemsToDisplay, configure and return a normal cell,
// otherwise, replace it with a button cell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
else {
}
if (indexPath.section == 0) {
elemento = [array objectAtIndex:indexPath.row];
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
labelTitle.text = [elemento objectForKey:#"Titolo"];
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textColor = [UIColor whiteColor];
[cell addSubview:labelTitle];
} else {
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
labelTitle.text = #"Read More";
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textColor = [UIColor whiteColor];
[cell addSubview:labelTitle];
}
return cell;
}
in this way I can see all the data on my table, but the label are overlap, than I try to use this method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"ItemCell";
// If the indexPath is less than the numberOfItemsToDisplay, configure and return a normal cell,
// otherwise, replace it with a button cell.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
if (indexPath.section == 0) {
elemento = [array objectAtIndex:indexPath.row];
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
labelTitle.text = [elemento objectForKey:#"Titolo"];
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textColor = [UIColor whiteColor];
[cell addSubview:labelTitle];
} else {
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
labelTitle.text = #"Read More";
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textColor = [UIColor whiteColor];
[cell addSubview:labelTitle];
}
}
else {
}
return cell;
}
the label are OK but in this case I can see on my table only 5 data, and this 5 data are repeat for some time...
For example if in the first case on my table I can see: 1,2,3,4,5,6,7,8,9,10,...
in the second case I seee: 1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,...
where is the problem?
add this code
for (UIView *view in [cell.contentView subviews])
{
[view removeFromSuperview];
}
before
if (indexPath.section == 0) {
elemento = [array objectAtIndex:indexPath.row];
UILabel *labelTitle = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 220, 30)];
labelTitle.text = [elemento objectForKey:#"Titolo"];
labelTitle.backgroundColor = [UIColor clearColor];
labelTitle.textColor = [UIColor whiteColor];
[cell addSubview:labelTitle];
Currently you add a label to the cell and the next time the cell is reused..the label is still there and you add a label on top of it.
The code you posted is specifying UITableViewCellStyleSubtitle as the cell style, which means each cell will a text label and detail text label available in its corresponding properties textLabel, and detailTextLabel. So there's no reason for you to allocate additional instances of UILabel. Instead, just populate the text properties of the existing labels. For example, you could rewrite your implementation like this:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellID = #"ItemCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellID];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellID];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor whiteColor];
}
cell.textLabel.text = (indexPath.section == 0 ?
[array objectAtIndex:indexPath.row] :
#"ReadMore");
return cell;
}
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]) {