I have the following method that loads cells into my table. The cell with text "No Results" is being repeated though as I scroll. What am I doing wrong with the dequeueing?
- (UITableViewCell *)tableView:(UITableView *)iTableView cellForRowAtIndexPath:(NSIndexPath *)iIndexPath {
static NSString *kCellID = #"cellID";
UITableViewCell *aCell = [iTableView dequeueReusableCellWithIdentifier:kCellID];
if (aCell == nil) {
aCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellID] autorelease];
}
if (iTableView == self.searchDisplayController.searchResultsTableView) {
if (self.isSearching) {
static NSString *aProgressIdentifier = #"SearchProgress";
aCell = [iTableView dequeueReusableCellWithIdentifier:aProgressIdentifier];
if (!aCell) {
aCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:aProgressIdentifier] autorelease];
}
aCell.userInteractionEnabled = NO;
UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectZero];
aLabel.text = #"Searching...";
aLabel.font = [UIFont systemFontOfSize:16.0f];
aLabel.textColor = [UIColor grayColor];
aLabel.textAlignment = UITextAlignmentCenter;
[aLabel sizeToFit];
aLabel.frame = CGRectMake(floorf((self.view.frame.size.width / 2.0f) - (aLabel.frame.size.width / 2.0f)), floorf((aCell.frame.size.height / 2.0f) - (aLabel.frame.size.height / 2.0f)), aLabel.frame.size.width, aLabel.frame.size.height);
[aCell addSubview:aLabel];
[aLabel release];
UIActivityIndicatorView *aSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[aCell addSubview:aSpinner];
aSpinner.frame = CGRectMake(floorf((aCell.frame.size.width / 2.0f) - (aSpinner.frame.size.width / 2.0f)) - floorf(aLabel.frame.size.width / 2.0f) - 20.0f, floorf((aCell.frame.size.height / 2.0f) - (aSpinner.frame.size.height / 2.0f)), aSpinner.frame.size.width, aSpinner.frame.size.height);
[aSpinner startAnimating];
[aSpinner release];
} else {
Class anObject = [self.masterArray objectAtIndex:iIndexPath.section];
if ([anObject isKindOfClass: [NSArray class]]) {
NSArray *sectionArray = [self.masterArray objectAtIndex:iIndexPath.section];
if (sectionArray.count > 0) {
id aCellObject = [sectionArray objectAtIndex:iIndexPath.row];
if ([aCellObject isKindOfClass:[CMACustomer class]]) {
CMACustomer *aCustomerObject = aCellObject;
aCell.textLabel.
text = aCustomerObject.customerFullName;
aCell.detailTextLabel.text = nil;
} else if ([aCellObject isKindOfClass:[CMAReservation class]]) {
CMAReservation *aReservationObject = aCellObject;
aCell.textLabel.text = aReservationObject.fullName;
aCell.detailTextLabel.text = aReservationObject.orderDescription;
// aCell.detailTextLabel.text = aReservationObject.reservationTimeAndDate;
}
} else {
aCell.userInteractionEnabled = NO;
UILabel *aLabel = [[UILabel alloc] initWithFrame:CGRectZero];
aLabel.text = #"No Results.";
aLabel.font = [UIFont systemFontOfSize:16.0f];
aLabel.textColor = [UIColor grayColor];
aLabel.textAlignment = UITextAlignmentCenter;
[aLabel sizeToFit];
aLabel.frame = CGRectMake(floorf((self.view.frame.size.width / 2.0f) - (aLabel.frame.size.width / 2.0f)), floorf((aCell.frame.size.height / 2.0f) - (aLabel.frame.size.height / 2.0f)), aLabel.frame.size.width, aLabel.frame.size.height);
[aCell addSubview:aLabel];
[aLabel release];
}
}
}
} else {
NSString *aLocalizedTitle = [[self.defaultScopeArray objectAtIndex:iIndexPath.row] objectForKey:#"localizedTitle"];
NSString *aDefaultTitle = [[self.defaultScopeArray objectAtIndex:iIndexPath.row] objectForKey:#"defaultTitle"];
aCell.textLabel.text = CMALocalizedStringWithDefaultValue(aLocalizedTitle, aDefaultTitle);
if ([[[self.currentScopeArray objectAtIndex:iIndexPath.row] objectForKey:#"enabledByDefault"] boolValue]) {
aCell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
aCell.accessoryType = UITableViewCellAccessoryNone;
}
}
return aCell;
}
A couple of things:
If you just want text labels in your cells there is no need to add a UILabel to the tableViewCell. Just use the textLabel property of the cell or the detailTextLabel property (depending on the tableviewcell type you're using).
If you do want to add a UILabel you should add it to the contentView property of the tableViewCell.
When you are adding views to a tableViewcell, you really shouldn't alloc/init it outside of the if(!Cell) {} block as every time you're recycling that tableViewcell, another another UILabel is alloc/init'd.
According to the code you posted and the problem you are seeing, it looks like sectionArrayis empty. Where is that being filled?
Related
I have a question about adding UIView in UIcollectionviewcell. I haveUIcollectionview and is the condition, if condition is true I need to add UIView to UIcollectionviewcell. If the condition is false don't add.
After updating UIcollectionview UIView is added to Cell for which conditions is true and some for which the condition is false. How to add a UIView to UIcollectionviewcell? one, two or three UIView can be added cell.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger dayStart = [self dayWeekStart:[self getCurentDate:indexPath.section]];
CalendarCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
if (indexPath.item+1 < dayStart) {
CalendarEmptyCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"cellemptyIdentifier" forIndexPath:indexPath];
return cell;
} else {
CalendarCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
cell.titleLabel.text = [NSString stringWithFormat:#"%i",indexPath.item-dayStart+2];
int todayDayRowInt = todayDayRow;
int dayPlusShift = todayDayRowInt + dayStart - 2;
cell.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:15];
CALayer* layer = [cell layer];
[layer setCornerRadius:15];
dateFormatter.dateFormat = #"yyyy-MM-dd hh:mm:ss Z";
cell.titleLabel.textColor = [UIColor lightGrayColor];
dateFromString1 = [NSDate date];
dateFromString2 = [dateFormatter dateFromString:_dateFinish];
dateFromString3 = [dateFormatter dateFromString:_dateStart];
if ([self checkDateFromCalendar:indexPath.section row:indexPath.item + 3 - dayStart] == 1) {
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[arrColor removeAllObjects];
for (int i=0; i<appDelegate.selectPill.count; i++) {
Pill *arrPill = [appDelegate.selectPill objectAtIndex:i];
NSNumber *mDay = arrPill.manyday;
int mDayInt = [mDay integerValue]+1;
if ([[self sameDateByAddingMonths:[dateFormatter dateFromString:_dateStart] addMonths:indexPath.section addDay:indexPath.item + 3 - dayStart] compare:[self sameDateByAddingMonths:arrPill.start addMonths:0 addDay:mDayInt]] == NSOrderedAscending) {
NSDate *verifiableDate = [self sameDateByAddingMonths:[dateFormatter dateFromString:_dateStart] addMonths:indexPath.section addDay:indexPath.item];
NSDate *verifiableDate2 = [self sameDateByAddingMonths:[dateFormatter dateFromString:_dateStart] addMonths:indexPath.section addDay:indexPath.item+1];
NSTimeInterval diff = [verifiableDate timeIntervalSinceDate:arrPill.start];
NSTimeInterval diff2 = [verifiableDate2 timeIntervalSinceDate:arrPill.start];
yui = (diff2 - diff);
NSInteger sss = diff / yui;
if (diff >= 0) {
float fff = fmod (sss , ([arrPill.frequency doubleValue]+1));
if (fff == 0) {
NSArray *components = [arrPill.color componentsSeparatedByString:#","];
CGFloat r = [[components objectAtIndex:0] floatValue];
CGFloat g = [[components objectAtIndex:1] floatValue];
CGFloat b = [[components objectAtIndex:2] floatValue];
CGFloat a = [[components objectAtIndex:3] floatValue];
UIColor *color = [UIColor colorWithRed:r green:g blue:b alpha:a];
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0+(i*10), (i*10), 10, 10)];
view.backgroundColor = [UIColor color];
[cell.contentView addSubview:view];
}
}
}
}
cell.titleLabel.textColor = [UIColor darkGrayColor];
} else {
cell.titleLabel.textColor = [UIColor lightGrayColor];
}
if ([dateFromString1 compare:dateFromString2] != NSOrderedDescending && [dateFromString1 compare:dateFromString3] != NSOrderedAscending) {
if (indexPath.section == todayMonthSection && indexPath.item == dayPlusShift){
cell.backgroundColor = [UIColor colorWithRed:60.0/255.0 green:162.0/255.0 blue:161.0/255.0 alpha:1];
cell.titleLabel.textColor = [UIColor whiteColor];
cell.titleLabel.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:15];
} else {
cell.backgroundColor = [UIColor whiteColor];
}
}
return cell;
}
return cell;
}
Add your view visually to your
StoryBoard or Xib file and make custom cell classes make a connections
Use below code snippet
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
PhotosCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
UIView *anotherView = [[UIView alloc]initWithFrame:CGRectMake(0.0, 0.0, 20.0, 20.0)];
UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0.0, 0.0, 50.0, 20.0)];
label.text = #"Hello";
[anotherView addSubview:label];
[cell.myView addSubview:anotherView];
if("Your condition is true")
{
cell.myView.hidden = YES;
}
else
{
cell.myView.hidden = YES;
}
return cell;
}
Use the cell.contentView to get access to the cell's UIView.
Example:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
MyRootView *view = [[MyRootView alloc]initWithFrame:CGRectMake(0, 0, 100, 100) someBusinessLogicParam:1];
[cell.contentView addSubview:view];
return cell;
}
I think would help is you have a root view which takes in your business logic as parameters. The business parameters will determine which views to display during the root view's initWithFrame.
Modified Example of MyRootView:
- (id)initWithFrame:(CGRect)frame someBusinessLogicParam:(NSInteger)bp
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.backgroundColor = [UIColor whiteColor];
if (bp == 0) {
UIView *yourChild1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
[self addSubview:yourChild1];
} else if (bp == 1) {
UIView *yourChild2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
[self addSubview:yourChild2];
}else if (bp == 3) {
UIView *yourChild3 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
[self addSubview:yourChild3];
UIView *yourChild4 = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
[self addSubview:yourChild4];
}
}
return self;
}
You can then use the cell.contentView to add in the MyRootView along with your logic to what gets displayed.
Here is the code I am struggling with, the first table cell doesnt display the accessary arrow, but other table cells work fine...
Below is the code for table cell1, other cells is also customized but work fine.
- (void) initialization
{
labelTitle = [[UILabel alloc] initWithFrame:CGRectZero];
labelTitle.font = [UIFont fontForMoreLikeResultTitle];
labelTitle.textColor = [UIColor blackColor];
labelTitle.numberOfLines = 1;
labelTitle.lineBreakMode = UILineBreakModeTailTruncation;
labelTitle.backgroundColor = [UIColor clearColor];
labelFulLAddress = [[UILabel alloc] initWithFrame:CGRectZero];
labelFulLAddress.font = [UIFont fontForMoreLikeResultDescription];
labelFulLAddress.textColor = [UIColor blackColor];
labelFulLAddress.numberOfLines = 1;
labelFulLAddress.lineBreakMode = UILineBreakModeTailTruncation;
labelFulLAddress.backgroundColor = [UIColor clearColor];
[[self contentView] addSubview:labelTitle];
[[self contentView] addSubview:labelFulLAddress];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
// Initialization code
[self initialization];
}
return self;
}
- (void) layoutSubviews
{
float xOffset = 20.0f;
float yOffset = 10.0f;
float currentUsedHeight = yOffset;
labelTitle.text = documentTitle;
labelTitle.frame = CGRectMake(xOffset, currentUsedHeight,
320.0f - 2 * xOffset, 60.0f);
[labelTitle sizeToFitHeight];
[labelTitle sizeToFitWidth];
labelFulLAddress.text = #"99999 Bellevue Way NE, Bellevue WA";
currentUsedHeight += (yOffset + labelTitle.frame.size.height);
labelFulLAddress.frame = CGRectMake(xOffset, currentUsedHeight, 320.0f - 2 * xOffset, 60.0f);
[labelFulLAddress sizeToFitHeight];
[labelFulLAddress sizeToFitWidth];
}
Below is the code in view controller:
- (UITableViewCell *) createResultTableCell1:(UITableView *)tableView
{
static NSString *CellIdentifier = #"FirstMoreLikeResultCell";
FirstResultTableCell *cell = (FristResultTableCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[MoreLikeTableCell1 alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.documentTitle = self.documentTitle;
return cell;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
if (indexPath.row == 0)
{
cell = [self createResultTableCell1:tableView];
}
else
{
cell = [self createResultTableCell2:tableView cellForRowAtIndexPath:indexPath];
}
return cell;
}
Call [super layoutSubviews] from within your overridden layoutSubviews.
I have a custom UITableViewCell as such:
#implementation CellWithThreeSubtitles
#synthesize title, subTitle1, subTitle2, subTitle3;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
self.textLabel.backgroundColor = self.backgroundColor;
self.textLabel.opaque = NO;
self.textLabel.textColor = [UIColor blackColor];
self.textLabel.highlightedTextColor = [UIColor whiteColor];
self.textLabel.font = [UIFont boldSystemFontOfSize:22.0];
}
return self;
}
- (void)layoutSubviews
{
[super layoutSubviews];
CGRect contentRect = self.contentView.bounds;
CGRect frame = CGRectMake(35.0, 0, contentRect.size.width, 50.0);
self.textLabel.frame = frame;
UILabel *subLabel1 = [[UILabel alloc] init];
frame = CGRectMake(35.0, 34.0, contentRect.size.width, 25.0);
subLabel1.font = [UIFont systemFontOfSize:18.0];
subLabel1.backgroundColor = [UIColor clearColor];
subLabel1.text = subTitle1;
subLabel1.opaque = NO;
subLabel1.frame = frame;
[self.contentView addSubview:subLabel1];
UILabel *subLabel2 = [[UILabel alloc] init];
frame = CGRectMake(35.0, 54.0, contentRect.size.width, 25.0);
subLabel2.font = [UIFont boldSystemFontOfSize:18.0];
subLabel2.backgroundColor = [UIColor clearColor];
subLabel2.text = subTitle2;
subLabel2.opaque = NO;
subLabel2.frame = frame;
[self.contentView addSubview:subLabel2];
UILabel *subLabel3 = [[UILabel alloc] init];
frame = CGRectMake(contentRect.size.width-100.0, 54.0, contentRect.size.width, 25.0);
subLabel3.font = [UIFont systemFontOfSize:18.0];
subLabel3.backgroundColor = [UIColor clearColor];
subLabel3.text = subTitle3;
subLabel3.opaque = NO;
subLabel3.frame = frame;
[self.contentView addSubview:subLabel3];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)dealloc {
[super dealloc];
[subTitle4 release];
[subTitle3 release];
[subTitle2 release];
[subTitle1 release];
[title release];
}
When I implement it in my UITableView like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
int section = [indexPath indexAtPosition:0];
static NSString *kCustomCellID = #"AppointmentCellID";
CellWithThreeSubtitles *cell = (CellWithThreeSubtitles *)[tableView dequeueReusableCellWithIdentifier:kCustomCellID];
if (cell == nil) {
cell = (CellWithThreeSubtitles *)[[[CellWithThreeSubtitles alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease];
}
switch (section) {
case 0:
if ([wineArray count]==0) {
cell.textLabel.text = #"No wine favorites selected yet";
}else{
cell.subTitle1 = [[wineArray objectAtIndex:indexPath.row] objectForKey:#"Varietal"];
cell.subTitle2 = [NSString stringWithFormat:#"Bin No. %#", [[wineArray objectAtIndex:indexPath.row] objectForKey:#"Bin"]];
cell.subTitle3 = [NSString stringWithFormat:#"$%#", [[wineArray objectAtIndex:indexPath.row] objectForKey:#"Price"]];
cell.textLabel.text = [NSString stringWithFormat:#"%# (%#)", [[wineArray objectAtIndex:indexPath.row] objectForKey:#"Name"], [[wineArray objectAtIndex:indexPath.row] objectForKey:#"Availability"]];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.userInteractionEnabled = NO;
break;
case 1:
if ([dinnerArray count]==0)
cell.textLabel.text = #"No dinner favorites selected yet";
else
cell.textLabel.text = [NSString stringWithFormat:#"%#", [[dinnerArray objectAtIndex:indexPath.row] objectForKey:#"Name"]];
break;
}
return cell;
}
the contents of the cell duplicates on top of itself every time the orientation of the device changes. Is it redrawing the cell every time the device rotates? And if so, How can I keep it from doing so?
You should be creating the subviews and adding them to your cell's subviews array in your -initWithStyle:reuseIdentifier: method. You only need to create the subviews once. The -layoutSubviews method is invoked repeatedly by the framework whenever the device orientation changes, to allow you to resize and/or move the subviews (plus make any other minor adjustments) to compensate for differences between the orientations.
I suspect layoutSubViews is being called on rotation, and re-drawing new labels on top of your old ones. If you remove any labels from the table cell at the beginning of layoutSubViews that would probably fix that issue, or you could create them in the init and then just position them in layoutSubViews.
I am getting memory leak at text fiels please suggest me how to resolve it.
I written the code in init() as:
eventTextField = nil;
eventTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 15, 300, 50)];
eventTextField.placeholder = #"Event Name";
[eventTextField setFont:[UIFont boldSystemFontOfSize:14]];
eventTextField.returnKeyType = UIReturnKeyDone;
eventTextField.keyboardAppearance = UIKeyboardAppearanceDefault;//Showing leak at this line
eventTextField.keyboardType = UIKeyboardTypeDefault;
eventTextField.delegate=self;
and I released it in dealloc method.
and in cellForRowIndex I am adding it as subview.
- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MasterViewIdentifier"];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:#"MasterViewIdentifier"] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UIView* elementView = [[UIView alloc] initWithFrame:CGRectMake(20,170,320,280)];
elementView.tag = 0;
elementView.backgroundColor=[UIColor clearColor];
[cell.contentView addSubview:elementView];
[elementView release];
}
UIView* elementView = [cell.contentView viewWithTag:0];
elementView.backgroundColor=[UIColor clearColor];
for(UIView* subView in elementView.subviews)
{
[subView removeFromSuperview];
}
if(indexPath.section == 0 && indexPath.row == 0)
{
CorkItAppDelegate* appDelegate = (CorkItAppDelegate*)[[UIApplication sharedApplication] delegate];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
wineNameTitleLabel.numberOfLines = 0;
[wineNameTitleLabel setFont:[UIFont boldSystemFontOfSize:14]];
if(appDelegate.isNewWine == YES)
{
wineNameTitleLabel.textColor = [UIColor blackColor] ;
wineNameTitleLabel.text = appDelegate.getNewWineName;
}
else
{
if([event.eventWineName length]>0)
{
wineNameTitleLabel.textColor = [UIColor blackColor] ;
wineNameTitleLabel.text = event.eventWineName;
}
else
{
wineNameTitleLabel.text = #"Wine Name";
}
}
[elementView addSubview:wineNameTitleLabel];
}
else if(indexPath.section == 1)
{
if(isRightButton == YES)
{
eventTextField.enabled = NO;
}
else
{
eventTextField.enabled = YES;
}
if([event.eventName length] > 0)
{
eventTextField.text = event.eventName;
}
else
{
eventTextField.text = #"";
}
[elementView addSubview:eventTextField];
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
Hope I get wucik response from your side.
Thanks in advance,
Monish.
If you still need the eventTextField. You should create a property and instance variable for it. So that, you can your class own the eventTextField and release it in the dealloc. Like this:
#interface A {
UITextField *eventTextField;
}
#property (nonatomic, retain) UITextField *eventTextField;
#end
#implementation A
#synthesize eventTextField;
- (id)init {
eventTextField = [[UITextField alloc]init];
}
- (void)dealloc {
[eventTextField release];
}
#end
you should release the UITextField after adding it to the subview
[foo addSubview:eventTextField];
[eventTextField release];
eventTextField = nil; is unnecessary btw as eventTextField is a ivar is already zeroed (pointer set to 0x0(nil))
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]) {