Scrolling TableView in PageViewController - ios

I have parent and child View controllers, in child view controller I have image view and table view. If table view's content size is less than screen size, it goes properly. But if content size is more than screen size, I can't scroll it.
In XCode Attributes Inspector I have these properties. http://i.stack.imgur.com/yuxZ1.png
My code to generate Table View content:
- (void)viewDidLoad {
[super viewDidLoad];
_mainArray = [self getAnswers];
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSUInteger)section {
int height = (_imageView.image == 0) ? 0 : 118;
return height;
}
- (NSMutableArray *)getAnswers {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"pdd.sqlite"];
const char * dbpath = [defaultDBPath UTF8String];
sqlite3_stmt *statement;
NSMutableArray *array = [[NSMutableArray alloc] init];
if (sqlite3_open(dbpath, &_pdd_ab) == SQLITE_OK) {
NSString *querySQL = [NSString stringWithFormat:#"SELECT RecNo, Picture, Question, Answer1, Answer2, Answer3, Answer4, Answer5, RightAnswer, Comment FROM paper_ab WHERE PaperNumber = \"%#\" AND QuestionInPaper = \"%d\"", _randomNumbers[_index] , (int)_index + 1];
const char * query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(_pdd_ab, query_stmt, -1, &statement, NULL) == SQLITE_OK) {
if (sqlite3_step(statement) == SQLITE_ROW) {
NSData *picture = [[NSData alloc] initWithBytes:sqlite3_column_blob(statement, 1) length: sqlite3_column_bytes(statement, 1)];
_imageView.image = [UIImage imageWithData:picture];
for (int i = 2; i < 10; i++) {
if (sqlite3_column_text(statement, i) != NULL) {
NSString *arrayelement = [[NSString alloc]
initWithUTF8String:
(const char *) sqlite3_column_text(statement, i)];
[array addObject:arrayelement];
}
}
sqlite3_finalize(statement);
} else {
NSLog(#"Rezultatov net!");
}
} else {
NSLog(#"Ne mogu vypolnit' zapros!");
}
sqlite3_close(_pdd_ab);
} else {
NSLog(#"Ne mogu ustanovit' soedinenie!");
}
return array;
}
- (NSUInteger)numberOfSectionInTableView:(UITableView *)tableView {
return 1;
}
- (NSUInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSUInteger)section {
return _mainArray.count - 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger rowNumber = [indexPath row];
static NSString * CellIdentifier = #"Cell";
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
if (rowNumber == 0) {
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.font = [UIFont italicSystemFontOfSize:15.0];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.text = [NSString stringWithFormat:#"%#", _mainArray[0]];
cell.textLabel.numberOfLines = 0;
} else {
cell.textLabel.font = [UIFont systemFontOfSize:15.0];
cell.textLabel.text = [NSString stringWithFormat:#"%ld. %#", (long)rowNumber, _mainArray[rowNumber]];
cell.textLabel.numberOfLines = 0;
}
UIView *backView = [[UIView alloc] initWithFrame:CGRectZero];
backView.backgroundColor = [UIColor clearColor];
cell.backgroundView = backView;
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
cell.textLabel.font, NSFontAttributeName,
nil];
CGRect textLabelSize = [cell.textLabel.text boundingRectWithSize:kExamenLabelFrameMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributesDictionary context:nil];
cell.textLabel.frame = CGRectMake(5, 5, textLabelSize.size.width, textLabelSize.size.height);
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger rowNumber = [indexPath row];
if ([self.rightAnswersArray containsObject:[NSNumber numberWithLong:_index + 1]] ) { // делаю красиво, если пользователь возвращается к вопросу, на который уже правильно ответил
self.tableView.allowsSelection = NO;
if (rowNumber == [_mainArray[_mainArray.count - 2] intValue]) { // если ответ правильный
cell.contentView.backgroundColor = [UIColor colorWithRed:0 / 255.0f green:152 / 255.0f blue:70 / 255.0f alpha:1.0f];
}
} else if ([self.wrongAnswersArray containsObject:[NSNumber numberWithLong:_index + 1]] ) { // делаю красиво, если пользователь возвращается к вопросу, на который уже НЕправильно ответил
long questnum = [self.wrongAnswersArray indexOfObject:[NSNumber numberWithInteger:_index + 1]];
self.tableView.allowsSelection = NO;
if (rowNumber != [_mainArray[_mainArray.count - 2] intValue] && [NSNumber numberWithLong:rowNumber] == [NSNumber numberWithInt:[[self.wrongAnswersSelectedArray objectAtIndex:questnum] intValue]]) { // если ответ НЕправильный
cell.contentView.backgroundColor = [UIColor colorWithRed:236 / 255.0f green:30 / 255.0f blue:36 / 255.0f alpha:1.0f];
}
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSUInteger rowNumber = [indexPath row];
if (rowNumber == 0) {
cell.contentView.backgroundColor = [UIColor whiteColor];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
} else {
if (rowNumber == [_mainArray[_mainArray.count - 2] intValue]) { // если ответ правильный
if ([settings boolForKey:#"needVibro"]) {
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate); // вибрация при правильном ответе
}
cell.contentView.backgroundColor = [UIColor colorWithRed:0 / 255.0f green:152 / 255.0f blue:70 / 255.0f alpha:1.0f];
self.tableView.allowsSelection = NO;
[self.rightAnswersArray addObject:[NSNumber numberWithLong:_index + 1]];
} else { // если ответ неправильный
cell.contentView.backgroundColor = [UIColor colorWithRed:236 / 255.0f green:30 / 255.0f blue:36 / 255.0f alpha:1.0f];
self.tableView.allowsSelection = NO;
[self.wrongAnswersArray addObject:[NSNumber numberWithLong:_index + 1]];
[self.wrongAnswersSelectedArray addObject:[NSNumber numberWithLong:rowNumber]];
}
}
NSUInteger wrongCount = _wrongAnswersArray.count;
NSUInteger rightCount = _rightAnswersArray.count;
NSLog(#"Номер вопроса - %d, правильных ответов - %d, неправильных ответов - %d", (int)_index + 1, (int)rightCount, (int)wrongCount);
if (rightCount + wrongCount == 20) {
[self writeStatisticsToBase];
[self getResultOfTest];
[_timer invalidate];
}
}
- (void)getResultOfTest {
[self performSegueWithIdentifier:#"ResultExamen" sender:self];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUInteger rowNumber = [indexPath row];
NSString *textLabel = [NSString stringWithFormat:#"%ld. %#", (long)rowNumber, _mainArray[rowNumber]];
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont systemFontOfSize:15.0f], NSFontAttributeName,
nil];
CGRect textLabelSize = [textLabel boundingRectWithSize:kExamenLabelFrameMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributesDictionary context:nil];
return kExamenDifference + textLabelSize.size.height;
}
If I change properties "Bounces" and "Bounce Vertically" of Scroll View to enabled, I can scroll table view, but it return to its original state. You can see this situation on YouTube http://www.youtube.com/watch?v=5rDd4oQQDB0
So, my question is how to scroll UITableView in UIPageViewController if table content size is more than screen size?

You have to set bottom space to super view auto layout to your table-view manage scrolling according to device screen size.Bottom space to super-view auto-layout adjust your table-view bottom according to device screen size.

Related

How to expand and collapse tableviewcell when you click on particular index using autoresize

I want to expand and collapse cell when I clicked on the cell at particular index.
I am using auto resize and the data in the cell is dynamic i.e cell size increase based on label content I am using autoresize not autolayouts.
When I click on the cell the cell size will increase and the size to be increase is also dynamic what depends on the label content.
Please find my below code
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSInteger numOfSections = 0;
if (tableView==_commentstbl_view) {
return commentsarray.count;
}
else
if (isclassifiedarray.count > 0)
{
_myadstabelview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
_myadstabelview.backgroundView = nil;
numOfSections = isclassifiedarray.count;
}
else
{
// UILabel *noDataLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, _notificationTblView.bounds.size.width, _notificationTblView.bounds.size.height)];
// noDataLabel.text = #"No Results Found!";
// noDataLabel.textColor = [UIColor blackColor];
// noDataLabel.numberOfLines = 4;
// noDataLabel.textAlignment = NSTextAlignmentCenter;
// _notificationTblView.backgroundView = noDataLabel;
_myadstabelview.separatorStyle = UITableViewCellSeparatorStyleNone;
}
return numOfSections;
return isclassifiedarray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
commentscell = (commentsTableViewCell*)[_commentstbl_view dequeueReusableCellWithIdentifier:#"commentsTableViewCell" forIndexPath:indexPath];
commentscell.selectionStyle = UITableViewCellSelectionStyleNone;
[commentscell.layer setCornerRadius:4.0f];
[commentscell.layer setMasksToBounds:YES];
commentscell.profileimg.layer.cornerRadius=4.0f;
commentscell.profileimg.layer.masksToBounds = YES;
NSString *mobilenumber=[NSString stringWithFormat:#"%#",[[commentsarray objectAtIndex:indexPath.section]valueForKey:#"clubber_mobile"]];
NSString *comment=[[commentsarray objectAtIndex:indexPath.section]valueForKey:#"comment"];
UIFont *font=[UIFont fontWithName:#"Montserrat" size:14.0];
CGFloat size1 = [self getLabelHeightForStringforcomment:commentscell.commentlbl.text font:font];
commentscell.commentlbl.frame=CGRectMake(commentscell.commentlbl.frame.origin.x, commentlbYposition, commentscell.commentlbl.frame.size.width, size1);
commentscell.commentdatelbl.frame=CGRectMake(commentscell.commentdatelbl.frame.origin.x, commentlbYposition+size1, commentscell.commentdatelbl.frame.size.width, commentscell.commentdatelbl.frame.size.height);
commentscell.providerdetailsbtnoutlet.frame=CGRectMake(commentscell.providerdetailsbtnoutlet.frame.origin.x, commentlbYposition+size1+commentscell.commentdatelbl.frame.size.height, commentscell.providerdetailsbtnoutlet.frame.size.width, commentscell.providerdetailsbtnoutlet.frame.size.height);
if (selectedIndex==-1) {
commentscell.providerdetails_view.hidden=YES;
}else{
commentscell.providerdetails_view.hidden=NO;
commentscell.providerdetails_view.frame=CGRectMake(commentscell.providerdetailsbtnoutlet.frame.origin.x, commentlbYposition+size1+commentscell.commentdatelbl.frame.size.height+commentscell.providerdetailsbtnoutlet.frame.size.height, commentscell.providerdetails_view.frame.size.width, Providerdrtailviewhight);
commentscell.providerdetails_view.backgroundColor=[UIColor whiteColor];
}
NSString *commentclubbername=[[commentsarray objectAtIndex:indexPath.section]valueForKey:#"clubber_name"];
NSString *commentclubberid=[NSString stringWithFormat:#"%#",[[commentsarray objectAtIndex:indexPath.section]valueForKey:#"clubberId"]];
NSString *commentposteddate=[NSString stringWithFormat:#"%#",[[commentsarray objectAtIndex:indexPath.section]valueForKey:#"posted_date"]];
NSString *imgUrl = [NSString stringWithFormat:#"%s/presignin/clubber/getImage?clubberId=%#",urlPath,commentclubberid];
NSURL *imageURL=[NSURL URLWithString:imgUrl];
commentscell.profileimg.imageURL=imageURL;
commentscell.usernamelbl.text=commentclubbername;
commentscell.commentlbl.text=comment;
commentscell.commentdatelbl.text=[Util formatDateAndTime1:commentposteddate];
commentscell.callbtn.tag = indexPath.section;
commentscell.msgbtn.tag = indexPath.section;
commentscell.providerdetailsbtnoutlet.tag=indexPath.section;
[commentscell.callbtn addTarget:self action:#selector(callbtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[commentscell.msgbtn addTarget:self action:#selector(messagebtnClicked:) forControlEvents:UIControlEventTouchUpInside];
// [commentscell.providerdetailsbtnoutlet addTarget:self action:#selector(providerdetailsbtnclick:) forControlEvents:UIControlEventTouchUpInside];
commentscell.providerdetailsbtnoutlet.userInteractionEnabled=NO;
NSString *experience=[NSString stringWithFormat:#"%#",[[commentsarray objectAtIndex:indexPath.section]valueForKey:#"experience"]];
NSArray* foo = [experience componentsSeparatedByString: #"."];
if (foo.count==1) {
NSString* year = [foo objectAtIndex: 0];
// NSString* month = [foo objectAtIndex: 1];
expersstr=[NSString stringWithFormat:#"%# Years, 00 Months",year];
commentscell.explbl.text=expersstr;
}else{
NSString* year = [foo objectAtIndex: 0];
NSString* month = [foo objectAtIndex: 1];
expersstr=[NSString stringWithFormat:#"%# Years, %# Months",year,month];
commentscell.explbl.text=expersstr;
}
providerdescr=[NSString stringWithFormat:#"%#",[[commentsarray objectAtIndex:indexPath.section]valueForKey:#"description"]];
commentscell.desclbl.text=providerdescr;
return commentscell;
}
- (CGFloat)tableView:(UITableView* )tableView heightForRowAtIndexPath:(NSIndexPath* )indexPath;
{
UIFont *font=[UIFont fontWithName:#"Montserrat" size:14.0];
// NSString *str1 = [[isclassifiedarray objectAtIndex:indexPath.section]valueForKey:#"description"];
if (tableView==_commentstbl_view) {
if (selectedIndex == indexPath.section)
{
CGFloat size1 = [self getLabelHeightForStringforcomment:[[commentsarray objectAtIndex:indexPath.section]valueForKey:#"comment"] font:font];
// commentscell.providerdetails_view.hidden=NO;
return 104+size1-20+45;
}
else{
CGFloat size1 = [self getLabelHeightForStringforcomment:[[commentsarray objectAtIndex:indexPath.section]valueForKey:#"comment"] font:font];
// commentscell.providerdetails_view.hidden=YES;
return 104+size1-20;
}
}
}
please find the img which is clear idea.
Thanks for quick responce

weird behavior on dynamic uitableviewcell height

Im trying to make dynamic uitableviewcell height for my custome cell.
the cell is subclassed for adding some background.
this is my uitableview controller class :
#define PADDING 23.0f
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSUInteger count = [self.entries count];
return count + _rowcount;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reuseIdentifier = #"PlaceholderCell2";
SubcategoryTableViewCell * sctvCell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (sctvCell == nil) {
sctvCell= [[SubcategoryTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
NSUInteger nodeCount = [self.entries count];
sctvCell.contentView.translatesAutoresizingMaskIntoConstraints = NO;
UILabel *label = (UILabel *)[sctvCell.contentView viewWithTag:1];
if (nodeCount > 0)
{
NewsFetchAppRecord *appRecord = [self.entries objectAtIndex:indexPath.row];
[label setText:appRecord.title];
NSDictionary *attributes = #{NSFontAttributeName: [UIFont fontWithName:#"B MyFont" size:14.0f]};
CGRect rect = [appRecord.title boundingRectWithSize:CGSizeMake(label.frame.size.height - PADDING * 5, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
CGRect newFrame = label.frame;
newFrame.size.height = rect.size.height;
label.frame = newFrame;
[label sizeToFit];
UIView *whiteRoundedCornerView = (UIView *)[sctvCell.contentView viewWithTag:1000];
CGRect newFrame2 = whiteRoundedCornerView.frame;
newFrame2.size.width = 300;
newFrame2.size.height = rect.size.height + 160;
[ whiteRoundedCornerView setFrame:newFrame2];
}
if ((unsigned long)indexPath.row == [self.entries count] - 1){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0),
^{
NewsFetchParseOperation *p = [[NewsFetchParseOperation alloc]init];
NewsFetchAppRecord *appRecord = [self.entries objectAtIndex:indexPath.row];
p.cat = appRecord.Category;
self.intindex = self.intindex + 1;
p.index = [NSString stringWithFormat:#"%d", (int)self.intindex];
p.lastid = appRecord.ids;
[p main];
dispatch_async(dispatch_get_main_queue(), ^(void)
{
[self.tableView beginUpdates];
NSMutableArray *indexPaths = [NSMutableArray array];
NSInteger currentCount = self.entries.count;
for (NSUInteger i = 0; i < p.appRecordList.count; i++) {
[indexPaths addObject:[NSIndexPath indexPathForRow:currentCount+i inSection:0]];
}
NSArray *temp_1 =[self.entries arrayByAddingObjectsFromArray:p.appRecordList];
self.entries = temp_1;
[self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationLeft];
[self.tableView endUpdates];
});
});
}
return sctvCell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reuseIdentifier = #"PlaceholderCell2";
SubcategoryTableViewCell * sctvCell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (sctvCell == nil) {
sctvCell= [[SubcategoryTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
NewsFetchAppRecord *appRecord = [self.entries objectAtIndex:indexPath.row];
UILabel *label = (UILabel *)[sctvCell.contentView viewWithTag:1];
NSDictionary *attributes = #{NSFontAttributeName: [UIFont fontWithName:#"B MyFont" size:14.0f]};
CGRect rect = [appRecord.title boundingRectWithSize:CGSizeMake(label.frame.size.height - PADDING * 5, MAXFLOAT)
options:NSStringDrawingUsesLineFragmentOrigin
attributes:attributes
context:nil];
return rect.size.height + PADDING * 6;
}
and my cell subclass :
- (void)awakeFromNib {
self.news_img.layer.cornerRadius = 4;
self.news_img.clipsToBounds = YES;
self.resource_icon_img.layer.cornerRadius = 4;
self.resource_icon_img.clipsToBounds = YES;
self.contentView.backgroundColor = [UIColor clearColor];
self.whiteroundcorner = [[UIView alloc] initWithFrame:CGRectMake(10,10,300,250)];
self.whiteroundcorner.backgroundColor = [UIColor whiteColor];
self.whiteroundcorner.layer.masksToBounds = NO;
self.whiteroundcorner.layer.cornerRadius = 3.0;
[self.whiteroundcorner.layer setShadowColor:[UIColor grayColor].CGColor];
self.whiteroundcorner.layer.shadowOffset = CGSizeMake(-1, 1);
self.whiteroundcorner.layer.shadowOpacity = 0.2;
self.whiteroundcorner.tag = 1000;
[self.contentView addSubview:self.whiteroundcorner];
[self.contentView sendSubviewToBack:self.whiteroundcorner];
}
im using story board for my table like this :
now problem is most of time the height calculated incorrectly.
also some time the height goes way beyond on cell and in the end of 10 cell
when i try to fetch new row the last cell apears incorrectly.
You will need to calculate the height for the cell after setting its content.
So something like this:
- (CGFloat)heightForCellAtIndexPath:(NSIndexPath *)indexPath {
static UITableViewCell *sizingCell = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sizingCell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier];
});
/* Method where you set the content of the cell */
[self configureCell: atIndexPath:indexPath];
return [self calculateHeightForConfiguredSizingCell:sizingCell];
}
- (CGFloat)calculateHeightForConfiguredSizingCell:(UITableViewCell *)sizingCell {
[sizingCell setNeedsDisplay];
[sizingCell layoutIfNeeded];
CGSize size = [sizingCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
return size.height;
}

SKSTableView Scrolling Issue

I am currently using this custom/ expandable tableview code called SKSTableView which can be found at Here
Here's the issue, when I fill the table with over 10 rows, the UI Locks up and the scrolling begins to be extremely laggy. I can't seem to be able to track down the issue, but I am thinking it has something to do with reusing the cell.
Here's some code that will hopefully help.
-SKSTableView hasn't changed
-Below is the implementation in the actual file /Home Screen of app
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
Bar *bar = [bars objectAtIndex:indexPath.row];
NSArray *friendsAtBar = (NSArray*)bar.friends;
static NSString *cellIdentifier = #"SKSTableViewCell";
SKSTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell){//If no cell, create a new one
cell = [[SKSTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
UIColor *cellBackgroundColor = [UIColor colorWithWhite:( 30/255.0) alpha:1.0];
UIColor *cellTextColor = [UIColor colorWithWhite:0.80 alpha:1.0];
UIImageView *bottomLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 73, 320, 2)];
bottomLine.backgroundColor = [UIColor colorWithWhite:( 50/255.0) alpha:1.0];
cell.nameLabel.textColor = cellTextColor;
cell.friendsLabel.textColor = cellTextColor;
cell.backgroundColor = cellBackgroundColor;
cell.selectionBanner.backgroundColor = cellBackgroundColor;
[cell addSubview:bottomLine];
[cell.goingButton addTarget:self
action:#selector(goingButtonPressed:)
forControlEvents:UIControlEventTouchUpInside];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.nameLabel.frame = nameStartFrame;
}
if (friendsAtBar != nil && friendsAtBar.count != 0)
cell.isExpandable = YES;
else
cell.isExpandable = NO;
cell.nameLabel.text = bar.name;
//cell.userInteractionEnabled = YES;
if (friendsAtBar.count == 1) // If only 1 friend is going, say friend, if more than 1 friend is going say friends
cell.friendsLabel.text = [NSString stringWithFormat:#"%lu friend going",(unsigned long)friendsAtBar.count];
else if (friendsAtBar.count > 1)
cell.friendsLabel.text = [NSString stringWithFormat:#"%lu friends going",(unsigned long)friendsAtBar.count];
else {
cell.friendsLabel.text = #"";
//cell.nameLabel.frame = CGRectMake(20, 26, 200, 23);
}
//cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.tag = indexPath.row;
[self deselectGoingButton:cell.goingButton];
if ([bar.objectID isEqualToString:[currentUser objectForKey:#"currentBarID"]] && [self under15Hours:[currentUser objectForKey:#"barTimeStamp"]])
{
goingToBarID = bar.objectID; // HERE
//NSLog(#"goingToBarID from cell: %#",goingToBarID);
selectedGoingToButton = cell.goingButton;
[self selectGoingButton:cell.goingButton];
}
cell.goingButton.tag = indexPath.row;
cell.selectionBanner.tag = indexPath.row;
return cell;
}
#pragma mark - TableView - Subrow
- (NSInteger)tableView:(SKSTableView *)tableView numberOfSubRowsAtIndexPath:(NSIndexPath *)indexPath {
if (bars != nil && bars.count != 0) {
Bar *bar = [bars objectAtIndex:indexPath.row];
return bar.friends.count;
}
else
return 0;
} // PARSE IMPACTED
- (UITableViewCell *)tableView:(UITableView *)tableView cellForSubRowAtIndexPath:(NSIndexPath *)indexPath {
//NSMutableDictionary *bar = [bars objectAtIndex:indexPath.row];
Bar *bar = [bars objectAtIndex:indexPath.row];
NSArray *friendsAtBar = (NSArray*)bar.friends;
//UIColor *cellBackgroundColor = [UIColor colorWithWhite:( 20/255.0) alpha:1.0];
//UIColor *cellTextColor = [UIColor colorWithWhite:0.85 alpha:1.0];
UIImageView *bottomLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, 73, 320, 2)];
bottomLine.backgroundColor = [UIColor colorWithWhite:( 50/255.0) alpha:1.0];
static NSString *cellIdentifier = #"FriendsGoingCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UIView *cellView;
UILabel *nameLabel;
UILabel *usernameLabel;
UIButton *callButton;
UIButton *textButton;
if (!cell){//If no cell, make a new one
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cellView = [self createSubRowView];
nameLabel = [cellView.subviews objectAtIndex:1];
usernameLabel = [cellView.subviews objectAtIndex:2];
usernameLabel.textColor = [UIColor colorWithWhite:0.7 alpha:1.0];
callButton = [cellView.subviews objectAtIndex:3];
textButton = [cellView.subviews objectAtIndex:4];
[cell addSubview:cellView];
[cell addSubview:bottomLine];
}
//cell.backgroundColor = cellBackgroundColor;
cell.tag = 2;
//PFUser *user = [friendsAtBar objectAtIndex:indexPath.subRow-1];
Friend *user = [friendsAtBar objectAtIndex:indexPath.subRow-1];
//UILabel *nameLabel = [cellView.subviews objectAtIndex:1];
//nameLabel.text = [[friendsAtBar objectAtIndex:indexPath.subRow-1] objectForKey:#"username"];
nameLabel.text = [NSString stringWithFormat:#"%#",user.compositeName];
//nameLabel.textColor = cellTextColor;
// UILabel *usernameLabel = [cellView.subviews objectAtIndex:2];
usernameLabel.text = user.userName;
//UIButton *callButton = [cellView.subviews objectAtIndex:3];
callButton.tag = indexPath.subRow-1;
//UIButton *textButton = [cellView.subviews objectAtIndex:4];
textButton.tag = indexPath.subRow-1;
return cell;
}
[self createSubView]
- (UIView *)createSubRowView {
UIView *cellView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, [rowHeight floatValue])];
UIColor *fontColor = [UIColor colorWithWhite:0.85 alpha:1.0];
UIView* bgview = [[UIView alloc] initWithFrame:cellView.frame];
bgview.backgroundColor = [UIColor colorWithWhite:0.15 alpha:1.0];
[cellView addSubview:bgview];
UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 16, 180, 23)];
nameLabel.textColor = fontColor;
nameLabel.font = [UIFont boldSystemFontOfSize:17];
[cellView addSubview:nameLabel];
UILabel *goingTimeStampLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 41, 180, 16)];
goingTimeStampLabel.textColor = fontColor;
goingTimeStampLabel.font = [UIFont systemFontOfSize:13];
[cellView addSubview:goingTimeStampLabel];
CGFloat x = 200;
CGFloat width = 55;
CGFloat height = [rowHeight floatValue];
CGFloat y = ([rowHeight floatValue] - height)/2;
UIButton *callButton = [[UIButton alloc] initWithFrame:CGRectMake(x, y, width, height)];
UIImage *callIcon = [UIImage imageNamed:#"call.png"];
[callButton setImage:callIcon forState:UIControlStateNormal];
[callButton addTarget:self
action:#selector(clickedCallButton:)
forControlEvents:UIControlEventTouchUpInside];
[cellView addSubview:callButton];
UIButton *textButton = [[UIButton alloc] initWithFrame:CGRectMake(x+width, y, width, height)];
UIImage *textIcon = [UIImage imageNamed:#"text.png"];
[textButton setImage:textIcon forState:UIControlStateNormal];
[textButton addTarget:self
action:#selector(clickedTextButton:)
forControlEvents:UIControlEventTouchUpInside];
[cellView addSubview:textButton];
return cellView;
}
SKSTableView.m
#import "SKSTableView.h"
#import "SKSTableViewCell.h"
#import "SKSTableViewCellIndicator.h"
#import <objc/runtime.h>
#pragma mark - NSArray (SKSTableView)
#interface NSMutableArray (SKSTableView)
- (void)initiateObjectsForCapacity:(NSInteger)numItems;
#end
#implementation NSMutableArray (SKSTableView)
- (void)initiateObjectsForCapacity:(NSInteger)numItems
{
for (NSInteger index = [self count]; index < numItems; index++) {
NSMutableArray *array = [NSMutableArray array];
[self addObject:array];
}
}
#end
#pragma mark - SKSTableView
#interface SKSTableView () <UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, strong) NSMutableArray *expandedIndexPaths;
#property (nonatomic, strong) NSMutableDictionary *expandableCells;
#end
#implementation SKSTableView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_shouldExpandOnlyOneCell = NO;
}
return self;
}
- (void)setSKSTableViewDelegate:(id<SKSTableViewDelegate>)SKSTableViewDelegate
{
self.dataSource = self;
self.delegate = self;
[self setSeparatorColor:[UIColor colorWithRed:236.0/255.0 green:236.0/255.0 blue:236.0/255.0 alpha:1.0]];
if (SKSTableViewDelegate)
_SKSTableViewDelegate = SKSTableViewDelegate;
}
- (void)setSeparatorColor:(UIColor *)separatorColor
{
[super setSeparatorColor:separatorColor];
[SKSTableViewCellIndicator setIndicatorColor:separatorColor];
}
- (NSMutableArray *)expandedIndexPaths
{
if (!_expandedIndexPaths)
_expandedIndexPaths = [NSMutableArray array];
return _expandedIndexPaths;
}
- (NSMutableDictionary *)expandableCells
{
if (!_expandableCells)
_expandableCells = [NSMutableDictionary dictionary];
return _expandableCells;
}
#pragma mark - UITableViewDataSource
#pragma mark - Required
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_SKSTableViewDelegate tableView:tableView numberOfRowsInSection:section] + [[[self expandedIndexPaths] objectAtIndex:section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (![self.expandedIndexPaths[indexPath.section] containsObject:indexPath]) {
NSIndexPath *tempIndexPath = [self correspondingIndexPathForRowAtIndexPath:indexPath];
SKSTableViewCell *cell = (SKSTableViewCell *)[_SKSTableViewDelegate tableView:tableView cellForRowAtIndexPath:tempIndexPath];
if ([[self.expandableCells allKeys] containsObject:tempIndexPath])
[cell setIsExpanded:[[self.expandableCells objectForKey:tempIndexPath] boolValue]];
[cell setSeparatorInset:UIEdgeInsetsZero];
if (cell.isExpandable) {
[self.expandableCells setObject:[NSNumber numberWithBool:[cell isExpanded]]
forKey:indexPath];
UIButton *expandableButton = (UIButton *)cell.accessoryView;
[expandableButton addTarget:tableView
action:#selector(expandableButtonTouched:event:)
forControlEvents:UIControlEventTouchUpInside];
if (cell.isExpanded) {
cell.accessoryView.transform = CGAffineTransformMakeRotation(M_PI);
} else {
if ([cell containsIndicatorView])
[cell removeIndicatorView];
}
} else {
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell removeIndicatorView];
cell.accessoryView = nil;
}
return cell;
} else {
NSIndexPath *indexPathForSubrow = [self correspondingIndexPathForSubRowAtIndexPath:indexPath];
UITableViewCell *cell = [_SKSTableViewDelegate tableView:(SKSTableView *)tableView cellForSubRowAtIndexPath:indexPathForSubrow];
cell.backgroundView = nil;
cell.backgroundColor = [self separatorColor];
cell.indentationLevel = 2;
return cell;
}
}
#pragma mark - Optional
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if ([_SKSTableViewDelegate respondsToSelector:#selector(numberOfSectionsInTableView:)]) {
NSInteger numberOfSections = [_SKSTableViewDelegate numberOfSectionsInTableView:tableView];
if ([self.expandedIndexPaths count] != numberOfSections)
[self.expandedIndexPaths initiateObjectsForCapacity:numberOfSections];
return numberOfSections;
}
return 1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
if ([_SKSTableViewDelegate respondsToSelector:#selector(tableView:titleForHeaderInSection:)])
return [_SKSTableViewDelegate tableView:tableView titleForHeaderInSection:section];
return nil;
}
#pragma mark - UITableViewDelegate
#pragma mark - Optional
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([_SKSTableViewDelegate respondsToSelector:#selector(tableView:didSelectRowAtIndexPath:)])
[_SKSTableViewDelegate tableView:tableView didSelectRowAtIndexPath:indexPath];
if ([_SKSTableViewDelegate respondsToSelector:#selector(tableView:didDeselectRowAtIndexPath:)])
[_SKSTableViewDelegate tableView:tableView didDeselectRowAtIndexPath:indexPath];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
SKSTableViewCell *cell = (SKSTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
if ([cell isKindOfClass:[SKSTableViewCell class]] && cell.isExpandable) {
cell.isExpanded = !cell.isExpanded;
NSIndexPath *_indexPath = indexPath;
if (cell.isExpanded && self.shouldExpandOnlyOneCell) {
_indexPath = [self correspondingIndexPathForRowAtIndexPath:indexPath];
[self collapseCurrentlyExpandedIndexPaths];
}
NSInteger numberOfSubRows = [self numberOfSubRowsAtIndexPath:_indexPath];
NSMutableArray *indexPaths = [NSMutableArray array];
NSInteger row = _indexPath.row;
NSInteger section = _indexPath.section;
for (NSInteger index = 1; index <= numberOfSubRows; index++) {
NSIndexPath *expIndexPath = [NSIndexPath indexPathForRow:row+index inSection:section];
[indexPaths addObject:expIndexPath];
}
if (cell.isExpanded) {
[self setIsExpanded:YES forCellAtIndexPath:_indexPath];
[self insertExpandedIndexPaths:indexPaths forSection:_indexPath.section];
} else {
[self setIsExpanded:NO forCellAtIndexPath:_indexPath];
[self removeExpandedIndexPaths:indexPaths forSection:_indexPath.section];
}
[self accessoryViewAnimationForCell:cell];
}
}
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
if ([_SKSTableViewDelegate respondsToSelector:#selector(tableView:accessoryButtonTappedForRowWithIndexPath:)])
[_SKSTableViewDelegate tableView:tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
[self.delegate tableView:tableView didSelectRowAtIndexPath:indexPath];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
CGFloat height = 0;
if ([_SKSTableViewDelegate respondsToSelector:#selector(tableView:heightForHeaderInSection:)])
height = [_SKSTableViewDelegate tableView:tableView heightForHeaderInSection:section];
return height;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] init];
if ([_SKSTableViewDelegate respondsToSelector:#selector(tableView:viewForHeaderInSection:)])
view = [_SKSTableViewDelegate tableView:tableView viewForHeaderInSection:section];
return view;
}
#pragma mark - SKSTableViewUtils
- (IBAction)expandableButtonTouched:(id)sender event:(id)event
{
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self];
NSIndexPath *indexPath = [self indexPathForRowAtPoint:currentTouchPosition];
if (indexPath)
[self tableView:self accessoryButtonTappedForRowWithIndexPath:indexPath];
}
- (NSInteger)numberOfSubRowsAtIndexPath:(NSIndexPath *)indexPath
{
return [_SKSTableViewDelegate tableView:self numberOfSubRowsAtIndexPath:[self correspondingIndexPathForRowAtIndexPath:indexPath]];
}
- (NSIndexPath *)correspondingIndexPathForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger index = 0;
NSInteger row = 0;
while (index < indexPath.row) {
NSIndexPath *tempIndexPath = [self correspondingIndexPathForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:indexPath.section]];
BOOL isExpanded = [[self.expandableCells allKeys] containsObject:tempIndexPath] ? [[self.expandableCells objectForKey:tempIndexPath] boolValue] : NO;
if (isExpanded) {
NSInteger numberOfExpandedRows = [_SKSTableViewDelegate tableView:self numberOfSubRowsAtIndexPath:tempIndexPath];
index += (numberOfExpandedRows + 1);
} else
index++;
row++;
}
return [NSIndexPath indexPathForRow:row inSection:indexPath.section];
}
- (NSIndexPath *)correspondingIndexPathForSubRowAtIndexPath:(NSIndexPath *)indexPath
{
NSInteger index = 0;
NSInteger row = 0;
NSInteger subrow = 0;
while (1) {
NSIndexPath *tempIndexPath = [self correspondingIndexPathForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:indexPath.section]];
BOOL isExpanded = [[self.expandableCells allKeys] containsObject:tempIndexPath] ? [[self.expandableCells objectForKey:tempIndexPath] boolValue] : NO;
if (isExpanded) {
NSInteger numberOfExpandedRows = [_SKSTableViewDelegate tableView:self numberOfSubRowsAtIndexPath:tempIndexPath];
if ((indexPath.row - index) <= numberOfExpandedRows) {
subrow = indexPath.row - index;
break;
}
index += (numberOfExpandedRows + 1);
} else
index++;
row++;
}
return [NSIndexPath indexPathForSubRow:subrow inRow:row inSection:indexPath.section];
}
- (void)setIsExpanded:(BOOL)isExpanded forCellAtIndexPath:(NSIndexPath *)indexPath
{
NSIndexPath *correspondingIndexPath = [self correspondingIndexPathForRowAtIndexPath:indexPath];
[self.expandableCells setObject:[NSNumber numberWithBool:isExpanded] forKey:correspondingIndexPath];
}
- (void)insertExpandedIndexPaths:(NSArray *)indexPaths forSection:(NSInteger)section
{
NSIndexPath *firstIndexPathToExpand = indexPaths[0];
NSIndexPath *firstIndexPathExpanded = nil;
if ([self.expandedIndexPaths[section] count] > 0) firstIndexPathExpanded = self.expandedIndexPaths[section][0];
__block NSMutableArray *array = [NSMutableArray array];
if (firstIndexPathExpanded && firstIndexPathToExpand.section == firstIndexPathExpanded.section && firstIndexPathToExpand.row < firstIndexPathExpanded.row) {
[self.expandedIndexPaths[section] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSIndexPath *updated = [NSIndexPath indexPathForRow:([obj row] + [indexPaths count])
inSection:[obj section]];
[array addObject:updated];
}];
[array addObjectsFromArray:indexPaths];
self.expandedIndexPaths[section] = array;
} else {
[self.expandedIndexPaths[section] addObjectsFromArray:indexPaths];
}
[self sortExpandedIndexPathsForSection:section];
// Reload TableView
[self insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
}
- (void)removeExpandedIndexPaths:(NSArray *)indexPaths forSection:(NSInteger)section
{
NSUInteger index = [self.expandedIndexPaths[section] indexOfObject:indexPaths[0]];
[self.expandedIndexPaths[section] removeObjectsInArray:indexPaths];
if (index == 0) {
__block NSMutableArray *array = [NSMutableArray array];
[self.expandedIndexPaths[section] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSIndexPath *updated = [NSIndexPath indexPathForRow:([obj row] - [indexPaths count])
inSection:[obj section]];
[array addObject:updated];
}];
self.expandedIndexPaths[section] = array;
}
[self sortExpandedIndexPathsForSection:section];
// Reload Tableview
[self deleteRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationTop];
}
- (void)collapseCurrentlyExpandedIndexPaths
{
NSArray *expandedCells = [self.expandableCells allKeysForObject:[NSNumber numberWithBool:YES]];
if (expandedCells.count > 0) {
NSIndexPath *indexPath = [expandedCells firstObject];
[self.expandableCells setObject:[NSNumber numberWithBool:NO] forKey:indexPath];
[self removeExpandedIndexPaths:[self.expandedIndexPaths[indexPath.section] copy] forSection:indexPath.section];
SKSTableViewCell *cell = (SKSTableViewCell *)[self cellForRowAtIndexPath:indexPath];
cell.isExpanded = NO;
[self accessoryViewAnimationForCell:cell];
}
}
- (void)sortExpandedIndexPathsForSection:(NSInteger)section
{
[self.expandedIndexPaths[section] sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
if ([obj1 section] < [obj2 section])
return (NSComparisonResult)NSOrderedAscending;
else if ([obj1 section] > [obj2 section])
return (NSComparisonResult)NSOrderedDescending;
else {
if ([obj1 row] < [obj2 row])
return (NSComparisonResult)NSOrderedAscending;
else
return (NSComparisonResult)NSOrderedDescending;
}
}];
}
- (void)accessoryViewAnimationForCell:(SKSTableViewCell *)cell
{
__block SKSTableViewCell *_cell = cell;
[UIView animateWithDuration:0.2 animations:^{
if (_cell.isExpanded) {
_cell.accessoryView.transform = CGAffineTransformMakeRotation(M_PI);
} else {
_cell.accessoryView.transform = CGAffineTransformMakeRotation(0);
}
} completion:^(BOOL finished) {
if (!_cell.isExpanded)
[_cell removeIndicatorView];
}];
}
#end
#pragma mark - NSIndexPath (SKSTableView)
static void *SubRowObjectKey;
#implementation NSIndexPath (SKSTableView)
#dynamic subRow;
- (NSInteger)subRow
{
id subRowObj = objc_getAssociatedObject(self, SubRowObjectKey);
return [subRowObj integerValue];
}
- (void)setSubRow:(NSInteger)subRow
{
id subRowObj = [NSNumber numberWithInteger:subRow];
objc_setAssociatedObject(self, SubRowObjectKey, subRowObj, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
+ (NSIndexPath *)indexPathForSubRow:(NSInteger)subrow inRow:(NSInteger)row inSection:(NSInteger)section
{
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
indexPath.subRow = subrow;
return indexPath;
}
#end

EXC_BAD_ACCESS on NSString stringWithFormat [duplicate]

am getting the following error while scrolling the UITableview.
EXC_BAD_ACCESS, -[CFString retain]: message sent to deallocated instance
how can i find the deallocated instance...?
this my noOfRowsinsection code
for(int i=0;i<size;i++)
{
NSString *CellIdentifier1;
if(universalApp==2)
{
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];
CellIdentifier1 = #"CustomThumbImageTableCell_iphone";
cell = [[[CustomThumbImageTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier1] autorelease];
//NSLog(#">>>>> Creating image >>>>>>>>");
//cell.thumbImageView = [[CustomImageView alloc] initWithFrame:CGRectMake(4, 4, 83, 101)];
cell.thumbImageView = [[[CustomImageView alloc] initWithFrame:CGRectMake(4, 4, 83, 101)] autorelease];
[imgViewArray addObject:cell.thumbImageView];
[cell.thumbImageView release];
[pool release];
}
and this my cellforRow code
enter code hereif(universalApp==2)
{
NSLog(#"iphone cell>>>>>>>>>>>>>>>>>>>>>>");
CustomThumbImageTableCell *cell = nil;
#try
{
static NSString *CellIdentifier = #"CustomThumbImageTableCell_iphone";
int currentRow = [indexPath row] ;//+ skippedItems;
//NSLog(#">>>>>>>>>>>>Current Table row = %d, %d, %d", loadedCellArray.count, currentRow, [Table numberOfRowsInSection:0]);
/*if(loadedCellArray.count > currentRow + 1 && [loadedCellArray objectAtIndex:(currentRow)] != nil)
{
NSLog(#"Image updated = %d", ( currentRow));
cell = (CustomThumbImageTableCell_iphone *) [loadedCellArray objectAtIndex:(currentRow )];
}
else
{*/
/*if(loadedCellArray.count > currentRow + 1 )// && [loadedCellArray objectAtIndex:(currentRow)] != nil)
// if( [loadedCellArray objectAtIndex:(currentRow)] != nil)
{
//NSLog(#"Image updated = %d", ( currentRow));
cell = (CustomThumbImageTableCell_iphone *) [loadedCellArray objectAtIndex:([indexPath row] )];
if(!isScrollingUp)
{
//scrollCount++;
[loadedCellArray removeLastObject];
}
isScrollingUp = YES;
}
else*/
{
static NSString *CellIdentifier = #"CustomThumbImageTableCell_iphone";
static NSString *CellIdentifier1 = #"CustomThumbImageTableCell_iphone";
//UITableViewCell *cell;
if(cell==nil)
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[[CustomThumbImageTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
else
{
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
cell = [[[CustomThumbImageTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier1] autorelease];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
NSLog(#">>>>>>>>>..... %d, %d, %#", imgViewArray.count, indexPath.row, (((int)imgViewArray.count- 1) < (int)indexPath.row) ? #"YES" : #"NO");
/*if(((int)imgViewArray.count- 1) < (int)indexPath.row)
{
// NSLog(#">>>>> Creating image >>>>>>>>");
cell.thumbImageView = [[CustomImageView alloc] initWithFrame:CGRectMake(4, 4, 83, 101)];
[imgViewArray addObject:cell.thumbImageView];
}
else {
cell.thumbImageView = (CustomImageView *) [imgViewArray objectAtIndex:indexPath.row];
}*/
//NSLog(#"img array = %#,%d",imgViewArray,imgViewArray.count);
cell.thumbImageView =[imgViewArray objectAtIndex:indexPath.row];
cell.thumbImageView.contentMode = UIViewContentModeScaleAspectFit;
[cell.contentView addSubview:cell.thumbImageView];
cell.thumbImageView.index = (int)indexPath.row;
#try
{
{
BOOL isFound = NO;
do
{
#try {
NSArray *array1 = [contentString componentsSeparatedByString:#"###"];
NSArray *array2 = [[array1 objectAtIndex:currentRow ] componentsSeparatedByString:#"##"];
NSString *str = [NSString stringWithFormat:#"%#", [array2 objectAtIndex:0]];
str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(104, 6, 141, 96)];
name.textAlignment = UITextAlignmentCenter;
name.font = [UIFont fontWithName:#"Arial" size:17.0];
name.numberOfLines = 3;
name.backgroundColor = [UIColor clearColor];
name.textColor = [UIColor whiteColor];
cell.catalogName = name;
[cell.catalogName setText:[NSString stringWithFormat:#"%#", str]];
[cell.contentView addSubview:cell.catalogName];
//[name release];
name = [[UILabel alloc] initWithFrame:CGRectMake(107, 89, 193, 21)];//107,89,193
name.textAlignment = UITextAlignmentCenter;
name.font = [UIFont fontWithName:#"Arial" size:13.0];
name.textColor = [UIColor whiteColor];
name.numberOfLines = 3;
name.backgroundColor = [UIColor clearColor];
name.alpha = 0.26;
cell.pageNo = name;
[cell.contentView addSubview:cell.pageNo];
//[name release];
if((searchId != 2 && (isLineNameSearchEnabled == 0)) || searchInCatalogFlag == 1)
//cell Page No//pageNoLabel
//NSLog(#"cell for row.......");
[cell.pageNo setText:[NSString stringWithFormat:#"Page No: %#", [array2 objectAtIndex:(array2.count - 2)]]];
{
}
NSLog(#" thumb image = %#",[NSString stringWithFormat:#"%#%#", baseURL, [array2 objectAtIndex:1]]);
//[cell.thumbImageView setImageURL:#"http://www.zoomcatalog.com/sites/default/files/catalogs/27705_Abex2010/images/thumbnails/Thumb-1.jpg"];
[cell.thumbImageView setImageURL:[NSString stringWithFormat:#"%#%#", baseURL, [array2 objectAtIndex:1]]];
[NSThread detachNewThreadSelector:#selector(initThread) toTarget:cell.thumbImageView withObject:nil];
[cell.thumbImageView setPageNo:((searchId == 2 || (isLineNameSearchEnabled && searchInCatalogFlag == 0)) ? 0 : (int)currentRow)];
UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(264, 33, 36, 34)];
img.contentMode = UIViewContentModeScaleToFill;
[img setImage:cellArrow];
[cell.contentView addSubview:img];
[img release];
[cell.catalogName release];
//>>>> [cell.pageNoLabel release];
[cell.thumbImageView release];
isFound = YES;
}
#catch (NSException * e) {
//skippedItems++;
currentRow = [indexPath row] + 1;
}
}
while (!isFound);
/*if(isScrollingUp)
{
//scrollCount++;
[loadedCellArray removeLastObject];
}
isScrollingUp = NO;
currentRow =indexPath.row;
// cell = (CustomThumbImageTableCell_iphone *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
//cell = (CustomThumbImageTableCell_iphone *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"CustomThumbImageTableCell_iphone" owner:self options:nil];
//cell = (CustomThumbImageTableCell_iphone *) [loadedCellArray objectAtIndex:(currentRow )];
cell = self.cell;
self.cell = nil;
}
NSLog(#"Image loaded from source= %d,", ( currentRow));
//if(loadedCellArray.count > currentRow + 1)
// NSLog(#"Image loaded from source cell array ref= %#", [loadedCellArray objectAtIndex:(currentRow)]);
/*if(currentRow == ([Table numberOfRowsInSection:0] - 1))
{
cell.catalogName.text = #"50 More";
NSLog(#">>>>>>>>>>>>>>>>>>>>> End cell....");
return cell;
}
/////
// if(true)
// return cell;
#try
{
{
BOOL isFound = NO;
do
{
#try {
NSArray *array1 = [contentString componentsSeparatedByString:#"###"];
NSArray *array2 = [[array1 objectAtIndex:currentRow] componentsSeparatedByString:#"##"];
NSString *str = [NSString stringWithFormat:#"%#", [array2 objectAtIndex:0]];
str = [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
// NSLog(#"&&&&&&&&&&&&&&&&& %#", str);
[cell.catalogName setText:[NSString stringWithFormat:#"%#", str]];
if((searchId != 2 && (isLineNameSearchEnabled == 0)) || searchInCatalogFlag == 1)
[cell.pageNo setText:[NSString stringWithFormat:#"Page No: %#", [array2 objectAtIndex:(array2.count - 2)]]];
// NSLog(#"Background color has been set....%d", i);
//NSLog(#")
//NSLog(#"Catalog %d = %#", currentRow, [array1 objectAtIndex:currentRow]);
// NSLog(#"Image URL = %#", [NSString stringWithFormat:#"%#%#", baseURL, [array2 objectAtIndex:1]]);
[cell.thumbImageView setImageURL:[NSString stringWithFormat:#"%#%#", baseURL, [array2 objectAtIndex:1]]];
//[cell.thumbImageView setContentString:contentString];
//if(searchId == 2)
// [cell.thumbImageView setContentString:[NSString stringWithFormat:#"%#", [array2 objectAtIndex:(array2.count - (isLineNameSearch ? 2 : 2))]]];
[cell.thumbImageView performSelectorOnMainThread:#selector(initThread) withObject:nil waitUntilDone:NO];
[cell.thumbImageView setPageNo:(searchId == 2 ? #"0" : [indexPath row])];
isFound = YES;
}
#catch (NSException * e) {
//skippedItems++;
currentRow = [indexPath row] + 1;
}
*/
// }
// while(!isFound);// && currentRow < [Table numberOfRowsInSection:0]);
//[imgViewArray addObject:cell.thumbImageView];
// [imgViewArray retain];
//[label setTextColor:[UIColor blackColor]];
//[label setTextAlignment:UITextAlignmentCenter];
/* UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(7 + ((i % 3) * (96 + 8)) , 9 + ((i / 3) * (140 + 10)), 98, 142)];
[label setBackgroundColor:[UIColor grayColor]];
[scrollView addSubview:label];
[imgViewArray addObject:imgView];
[scrollView addSubview:imgView];
if(i % 3 == 0)
{
[scrollView setContentSize:CGSizeMake(scrollView.contentSize.width, scrollView.contentSize.height + (140 + 10))];
}*/
}
//[imgViewArray retain];
// [cellArray retain];
}
When you have memory management issues, there are a number of things you can do:
Re-read the Cocoa memory management rules and make sure that you're following them.
Run the static analyser. This will often pick up places where you have neglected the memory management rules.
Since it appears that you have already enabled NSZombies, insert a breakpoint for [_NSZombie release]. This should hopefully break on the line that is causing the problem.
Edit:
Based on your code, the following line could be the culprit.
[cell.thumbImageView release]; // NOT GOOD
Similarly in your other code you have:
[cell.thumbImageView release]; // NOT GOOD
[cell.catalogName release]; // NOT GOOD
You set an autoreleased object to the thumbImageView and catalogName property of the cell, but you are still trying to release it. This is not how Cocoa memory management works. You don't own cell.thumbImageView or cell.catalogName, so you should not release them.
This problem occurs when you are trying to access the deallocated object. May be your string get released before you accessing it.

Section on top of section iphone table

I am building a sectioned table, and it is showing up with what looks to be a section on top of a section. You can see on the image that there is a white line under each section.
The image
Here is the code I have to build the table:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [alertList count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier;
if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
CellIdentifier = #"CellLandscape";
}
else
{
CellIdentifier = #"Cell";
}
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier]
autorelease];
//cell.contentView.backgroundColor = [UIColor blackColor];
CGRect frame;
if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight)
{
frame.origin.x = 370;
//titleFrame.size.width = 320;
}
else
{
frame.origin.x = 220;
//titleFrame.size.width = 220;
}
frame.origin.y = 5;
frame.size.height = 15;
frame.size.width = 74;
UILabel *instLabel = [[UILabel alloc] initWithFrame:frame];
instLabel.tag = 1;
[cell.contentView addSubview:instLabel];
[instLabel release];
}
// Configure the cell.
Alert *p = [alertList objectAtIndex:indexPath.section];
UILabel *instLabel = (UILabel *) [cell.contentView viewWithTag:1];
instLabel.text = [p docDate];
instLabel.textColor = [UIColor blackColor];
[instLabel setFont:[UIFont fontWithName:#"Arial" size:12]];
NSString *path;
if ([[p subscription] isEqual:#"Y"])
{
path = [[NSBundle mainBundle] pathForResource:#"watchlist_on" ofType:#"png"];
}
else
{
path = [[NSBundle mainBundle] pathForResource:#"watchlist_off" ofType:#"png"];
}
//NSLog(#"%#", path);
cell.textLabel.textColor = [UIColor colorWithRed:.847 green:0 blue: 0 alpha: 1];
[cell.textLabel setFont:[UIFont boldSystemFontOfSize:13]];
cell.textLabel.text = [[NSString alloc] initWithFormat:#"%#", [p Name]];
cell.detailTextLabel.textColor = [UIColor blackColor];
[cell.detailTextLabel setFont:[UIFont fontWithName:#"Arial" size: 10]];
cell.detailTextLabel.text = [p docTitle];
cell.detailTextLabel.textColor = [UIColor blackColor];
[cell.detailTextLabel setFont:[UIFont fontWithName:#"Arial" size: 10]];
//cell.detailTextLabel.text = [p docDate];
//cell.imageView.image = [UIImage imageWithContentsOfFile:path];
cell.imageView.userInteractionEnabled = YES;
cell.imageView.userInteractionEnabled = YES;
//cell.imageView.image = [UIImage imageWithContentsOfFile:path];
/// [cell addSubview:imgView];
//cell.textLabel.text = [[NSString alloc] initWithFormat:#"%#, %# G: %#\nDOB: %# Inst: %#", [p lastName], [p firstName], [p gender],
// [p birthDate], [p inst]];
cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;
//only 1 alert at a time
//make doc list from alert object
Document *documentList1 = [[Document alloc] init];
self.documentList = documentList1;
[documentList1 setTitle:[p docTitle]];
[documentList1 setUniqueId:[p uniqueId]];
[documentList1 setDate:[p docDate]];
[documentList1 setRepoOID:[p repoOid]];
[documentArray addObject:documentList];
return cell;
}
Change the table view's separator style to either single line or none - you probably have it on single line etched.

Resources