UIButton not select the correct cell - ios

in my tableview I have a button in the custom cell
I used the delegates to take an action to the button
The button image changes only on the selected cell
My problem is this:
When I press the button happens:
the selected cell is working properly and the button changes correctly (correct)
the second immediately after the selected cell does not change the button (corrected)
the third cell repeats the action of the selected cell (wrong)
This is repeated endlessly do not understand why
 The button should change only the selected cell and not on other non-selected
Can you help me please?
-(void)ButtonPressedGoPoint:(FFCustomCellWithImage *)custom button:(UIButton *)gopointpressed {
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:#"it_IT"]];
NSNumber *likeCount = [numberFormatter numberFromString:custom.CountGoPoint.text];
if (!custom.AddGoPoint.selected) {
NSLog(#"TastoSelezionato");
likeCount = [NSNumber numberWithInt:[likeCount intValue] + 1];
custom.CountGoPoint.text = [NSString stringWithFormat:#"%#", likeCount];
[custom.AddGoPoint setBackgroundImage:[UIImage imageNamed:#"FFIMG_Medal_Blu"] forState:UIControlStateNormal];
}
else {
if ([likeCount intValue] > 0) {
likeCount = [NSNumber numberWithInt:[likeCount intValue] - 1];
custom.CountGoPoint.text = [NSString stringWithFormat:#"%#", likeCount];
}
NSLog(#"TastoDeselezionato");
[custom.AddGoPoint setBackgroundImage:[UIImage imageNamed:#"FFIMG_MedalADD"] forState:UIControlStateNormal];
}
custom.AddGoPoint.selected = !custom.AddGoPoint.selected;
}
This is my implementation of the tableview
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
self.PostDetails = [ArrayforPost objectAtIndex:indexPath.row];
static NSString *IdentificazioneCellaIMG = #"CellaIMG";
FFCustomCellWithImage * CellaIMG = (FFCustomCellWithImage * )[self.FFTableView dequeueReusableCellWithIdentifier:IdentificazioneCellaIMG];
if (CellaIMG == nil) {
CellaIMG = [[FFCustomCellWithImage alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:IdentificazioneCellaIMG ];
}
CellaIMG.delegate = self;
CellaIMG.tag = indexPath.row;
[CellaIMG.AddGoPoint setTag:indexPath.row];
if ([self AssignedGoPointToPost:self.PostDetails]) {
CellaIMG.AddGoPoint.selected =YES;
[CellaIMG.AddGoPoint setImage:[UIImage imageNamed:#"FFIMG_Medal_Blu"] forState:UIControlStateNormal];
} else {
CellaIMG.AddGoPoint.selected =NO;
[CellaIMG.AddGoPoint setImage:[UIImage imageNamed:#"FFIMG_MedalADD"] forState:UIControlStateNormal];
}
NSString *FotoPostSocial = [self.PostDetails objectForKey:FF_POST_IMMAGINE];
CellaIMG.FotoPost.file = (PFFile *)FotoPostSocial;
[CellaIMG.FotoPost loadInBackground];
CellaIMG.FotoPost.layer.masksToBounds = YES;
CellaIMG.FotoPost.layer.cornerRadius = 2.0f;
CellaIMG.FotoPost.contentMode = UIViewContentModeScaleAspectFill;
CellaIMG.backgroundCell.layer.masksToBounds = YES;
CellaIMG.backgroundCell.layer.cornerRadius = 2.0f;
CellaIMG.backgroundCell.layer.borderColor = [UIColor colorWithRed:(219/255.0) green:(219/255.0) blue:(219/255.0) alpha:(1)].CGColor;
CellaIMG.backgroundCell.layer.borderWidth = 1.0f;
CellaIMG.backgroundCell.autoresizingMask = UIViewAutoresizingFlexibleHeight;
CellaIMG.LeggiCommentoButton.layer.cornerRadius = 2.0f;
CellaIMG.FotoProfilo.layer.masksToBounds = YES;
CellaIMG.FotoProfilo.layer.cornerRadius = 25.0f;
CellaIMG.FotoProfilo.contentMode = UIViewContentModeScaleAspectFill;
CellaIMG.TestoPost.font = [UIFont fontWithName:#"Helvetica" size:14.0f];
NSString *text = [self.PostDetails objectForKey:FF_POST_TEXT];
CellaIMG.TestoPost.text = text;
[CellaIMG.TestoPost setLineBreakMode:NSLineBreakByTruncatingTail];
NSString *NomeUser = [[self.PostDetails objectForKey:FF_POST_UTENTE] objectForKey:FF_USER_NOMECOGNOME];
CellaIMG.NomeUtente.text = NomeUser;
NSString *ImmagineUtente = [[self.PostDetails objectForKey:FF_POST_UTENTE] objectForKey:FF_USER_FOTOPROFILO];
CellaIMG.FotoProfilo.file = (PFFile *)ImmagineUtente;
[CellaIMG.FotoProfilo loadInBackground];
if (CellaSelezionata == indexPath.row) {
CGFloat AltezzaLabel = [self valoreAltezzaCella: indexPath.row];
CellaIMG.TestoPost.frame = CGRectMake(CellaIMG.TestoPost.frame.origin.x, CellaIMG.TestoPost.frame.origin.y, CellaIMG.TestoPost.frame.size.width, AltezzaLabel); }
else {
CellaIMG.TestoPost.frame = CGRectMake(CellaIMG.TestoPost.frame.origin.x, CellaIMG.TestoPost.frame.origin.y, CellaIMG.TestoPost.frame.size.width, 65);
}
return CellaIMG;
}
}

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

How to change image on button pressed with cell reused

There is a problem. I need at the touch of a button to add to the array and at the button to change the image.
I do it this way:
- (IBAction)addUserAction:(UIButton *)sender {
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:self.tableView];
NSIndexPath *clickedButtonIndexPath = [self.tableView indexPathForRowAtPoint:touchPoint];
NSInteger row = clickedButtonIndexPath.row;
UserCell* selectedCell = [self.tableView cellForRowAtIndexPath:clickedButtonIndexPath];
NSString* login = selectedCell.loginLabel.text;
BOOL userIsExist = selectedCell.isExist;
for (QBUUser* user in self.users) {
if ([login isEqualToString:user.login]) {
if (!userIsExist) {
[self.assistant.usersInCurrentChat addObject:user];
userIsExist = YES;
UIButton* button = self.buttons[clickedButtonIndexPath.row];
[button setImage:[UIImage imageNamed:#"delete_icon_64x64.png"] forState:UIControlStateNormal];
self.buttons[clickedButtonIndexPath.row] = button;
} else{
[self.assistant.usersInCurrentChat removeObject:user];
userIsExist = NO;
UIButton* button = self.buttons[clickedButtonIndexPath.row];
[button setImage:[UIImage imageNamed:#"add_button_64x64.png"] forState:UIControlStateNormal];
self.buttons[clickedButtonIndexPath.row] = button;
}
}
}
selectedCell.isExist = userIsExist;
}
my Code for cellForRowAtIndexPath:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UserCell* cell = [tableView dequeueReusableCellWithIdentifier:#"userCell1"];
if(!cell){
cell = [[UserCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"userCell1"];
}
QBUUser* user = [self.users objectAtIndex:indexPath.row];
cell.loginLabel.text = [NSString stringWithFormat:#"%#", user.login];
NSInteger currentTimeInterval = [[NSDate date] timeIntervalSince1970];
NSInteger userLastRequestAtTimeInterval = [[user lastRequestAt] timeIntervalSince1970];
// if user didn't do anything last 1 minute (60 seconds)
if((currentTimeInterval - userLastRequestAtTimeInterval) > 60){
cell.statusImageView.image = [UIImage imageNamed:#"offline_icon_45x45.png"];
} else{
cell.statusImageView.image = [UIImage imageNamed:#"online_icon_32x32.png"];
}
NSInteger row = indexPath.row;
if ([self.buttons[indexPath.row] isEqual:[NSNull null]]) {
self.buttons[indexPath.row] = cell.button;
} else {
cell.button = self.buttons[indexPath.row];
}
return cell;
}
But the cell "reused" and I get the result as in the picture my bug
How do I fix this ?

UiSegementedController's segment automatically select in UITableViewCell when scroll the uitableview

I have added UISegmentedController to TableViewCell in CellForRowAtIndexPath.
Following is the code.
[cell.segStatus addTarget:self action:#selector(selectedSegmentControl:) forControlEvents: UIControlEventValueChanged];
[cell.segStatus setSegmentedControlStyle:UISegmentedControlStyleBar];
cell.segStatus.tag = indexPath.row;
I use UITableViewCell class for customized class.
I select segment and when I scroll the tableview some other cell's segmented controller also automatically select.
Why is it? how can I sort out this ?
Full CellForRowAtIndexPath Method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
#try {
static NSString *defTableIdentifier = #"DefectRegisterCell";
DefectRegisterCell *cell = (DefectRegisterCell *)[self.tblDefectList dequeueReusableCellWithIdentifier:defTableIdentifier];
_defectEntry = [defectObjList objectAtIndex:indexPath.row];
cell.lblDefectNo.text = [NSString stringWithFormat:#"%#", _defectEntry.defectNo];
if ([_defectEntry.defect isEqual:#"None"] || [_defectEntry.defect isEqual:#""] ) {
cell.lblDefect.text = [NSString stringWithFormat:#"%# - %# - Passed", _defectEntry.item,_defectEntry.subItem];
}
else{
cell.lblDefect.text = [NSString stringWithFormat:#"%# - %# - %#", _defectEntry.item,_defectEntry.subItem,_defectEntry.defect];
}
cell.lblLocation.text = #"Location:";
cell.lblLocationValue.text = [NSString stringWithFormat:#"%# > %# > %# > %# (%# - %#)",_defectEntry.building, _defectEntry.area, _defectEntry.location, _defectEntry.room,_defectEntry.orient, _defectEntry.position];
cell.lblAssignedTo.text = #"Assigned to:";
cell.lblAssignedToValue.text = _defectEntry.responsibleComp;
cell.lblDue.text = #"Due:";
cell.lblDueValue.text = _defectEntry.dueDate;
cell.lblCapturedBy.text = #"Recorded by:";
cell.lblCapturedByValue.text = [NSString stringWithFormat:#"%# (%#)",_defectEntry.inspectedId,_defectEntry.inspectedCompId];
cell.lblDate.text = #"On:";
cell.lblDateValue.text = _defectEntry.createdDate;
NSString *attached = #"0" ;
NSString *fixClaimed = #"0";
NSString *completed = #"0";
if ([_defectEntry.attached length] > 0) {
attached = #"1";
}
if ([_defectEntry.fixClaimed isEqualToString:#"Y"]) {
fixClaimed = #"1";
}
if ([_common checkDateValidation:_defectEntry.completed]) {
completed = #"1";
}
[cell.segStatus addTarget:self action:#selector(selectedSegmentControl:) forControlEvents: UIControlEventValueChanged];
[cell.segStatus setSegmentedControlStyle:UISegmentedControlStyleBar];
cell.segStatus.tag = indexPath.row;
if ([attached isEqualToString:#"1"]) {
cell.imgAttachment.image = [UIImage imageNamed:#"diAttachments.png"];
}
if ([fixClaimed isEqualToString:#"1"]) {
cell.imgDefect.image = [UIImage imageNamed:#"diWarning.png"];
cell.segStatus.hidden = NO;
} else if ([completed isEqualToString:#"1"]) {
cell.imgDefect.image = [UIImage imageNamed:#"diRight.png"];
if ([_defectEntry.passed isEqualToString:#"Yes"]) {
cell.segStatus.hidden = YES;
}
else{
cell.segStatus.hidden = NO;
}
} else {
cell.imgDefect.image = [UIImage imageNamed:#"diWrong.png"];
cell.segStatus.hidden = NO;
}
UIView *backgroundView = [[UIView alloc] init];
if (indexPath.row % 2) {
backgroundView.backgroundColor = [UIColor whiteColor];
} else {
backgroundView.backgroundColor = [UIColor colorWithRed:242/255.0f green:242/255.0f blue:242/255.0f alpha:1.0f];
}
cell.backgroundView = backgroundView;
//change the selected cell color
UIView *selectedBackgroundView = [[UIView alloc] init];
selectedBackgroundView.backgroundColor = gaSelectedColor;
cell.selectedBackgroundView = selectedBackgroundView;
return cell;
}
#catch(NSException *excp)
{
NSLog(#"%#",excp);
}
}

UIImage in UITableView displaying the other cell's image for a second

I am trying to load image asynchronously in a UIImage that resides in a table view cell. sometimes it shows the image from another cell for a second before it displays the correct one.
This is the code I am using
[[TMCache sharedCache] objectForKey:post[#"gif"] block:^(TMCache *cache, NSString *key, id object) {
if (object) {
if ([self isRowZeroVisible:indexPath.section]) {
[GIFLoader loadGIFData:object to:postGif for:feedTableView];
}
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
__block NSURL* url = [NSURL URLWithString:post[#"gif"]];
NSURLRequest* req = [NSURLRequest requestWithURL:url];
OHURLLoader* loader = [OHURLLoader URLLoaderWithRequest:req];
[loader startRequestWithResponseHandler:nil
progress:nil
completion:^(NSData* receivedData, NSInteger httpStatusCode) {
if ([self isRowZeroVisible:indexPath.section]) {
[GIFLoader loadGIFData:receivedData to:postGif for:feedTableView];
}
[[TMCache sharedCache] setObject:receivedData forKey:post[#"gif"]];
} errorHandler:nil];
});
}];
[GIFLoader loadGIFData:receivedData to:postGif for:feedTableView]; is a method I created that loads GIFs in a uiimageview. it does that in a background thread and then assigns the Image in the main thread though.
Are there certain practices that I need to follow for async loading?
I am not sure what is wrong with my code. any help would be highly appreciated
edit
//Loading stuff into tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
#autoreleasepool {
//Type of Cells
static NSString *simpleTableImageIdentifier = #"gifCell";
static NSString *simpleTableBodyIdentifier = #"bodyCell";
static NSString *simpleTableActionsIdentifier = #"actionsCell";
UITableViewCell *cell = nil;
//Data that goes in the cell
NSDictionary *post = [dataArray objectAtIndex:indexPath.section];
//User info
UIImageView *userAvatar;
UILabel *postUserFullNameLabel;
UILabel *postUsername;
//GIF Date
OHAttributedLabel* gifDate = nil;
NSMutableAttributedString* gifDateString;
//Feed GIF
NSString *gifBody = [[NSString alloc]init];
OHAttributedLabel* attrLabel = nil;
NSMutableAttributedString* mas;
//Location
OHAttributedLabel* gifLocation = nil;
NSMutableAttributedString* gifLocationString;
UILabel *locationBg;
//Buttons
UIButton *btn1 =[[UIButton alloc] initWithFrame:CGRectMake(11,0,50,30)];
UIButton *btn2 =[[UIButton alloc] initWithFrame:CGRectMake(69,0,50,30)];
UIButton *btn3 =[[UIButton alloc] initWithFrame:CGRectMake(259,0,50,30)];
if (indexPath.row == 0) {
cell = [tableView dequeueReusableCellWithIdentifier:simpleTableImageIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableImageIdentifier];
}
__block UIImageView *postGif= (UIImageView *)[cell viewWithTag:104];
postGif.image = nil;
[[TMCache sharedCache] objectForKey:post[#"gif"] block:^(TMCache *cache, NSString *key, id object) {
if (object) {
if ([self isRowZeroVisible:indexPath.section]) {
[GIFLoader loadGIFData:object to:postGif for:feedTableView];
}
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
__block NSURL* url = [NSURL URLWithString:post[#"gif"]];
NSURLRequest* req = [NSURLRequest requestWithURL:url];
OHURLLoader* loader = [OHURLLoader URLLoaderWithRequest:req];
[loader startRequestWithResponseHandler:nil
progress:nil
completion:^(NSData* receivedData, NSInteger httpStatusCode) {
if ([self isRowZeroVisible:indexPath.section]) {
[GIFLoader loadGIFData:receivedData to:postGif for:feedTableView];
}
[[TMCache sharedCache] setObject:receivedData forKey:post[#"gif"]];
} errorHandler:nil];
});
}];
postGif.layer.cornerRadius = 2.0;
postGif.layer.masksToBounds = YES;
postGif.clipsToBounds = YES;
locationBg = (UILabel *)[cell viewWithTag:106];
userAvatar = (UIImageView *)[cell viewWithTag:100];
if ([post [#"user_avatar"] length ] > 0) {
NSString *img = [#"https://d1f7i732a4e7fw.cloudfront.net/" stringByAppendingString:post [#"user_avatar"]];
[userAvatar setImageWithURL:[NSURL URLWithString:img] placeholderImage:[UIImage imageNamed:#"userDefaultPicture.png"]];
}else{
userAvatar.image = [UIImage imageNamed:#"userDefaultPicture.png"];
}
userAvatar.layer.cornerRadius = 18.0;
userAvatar.layer.borderWidth = 1.0;
userAvatar.layer.borderColor = (__bridge CGColorRef)([UIColor lightGrayColor]);
userAvatar.clipsToBounds = YES;
postUserFullNameLabel = (UILabel *)[cell viewWithTag:101];
postUserFullNameLabel.text = post[#"user_fullname"];
postUsername = (UILabel *)[cell viewWithTag:102];
postUsername.text = [NSString stringWithFormat:#"#%#",post[#"user_username"]];
gifDate = [[OHAttributedLabel alloc] initWithFrame:CGRectMake(246,9,60,21)];
gifDate.autoresizingMask = UIViewAutoresizingNone;
gifDate.centerVertically = YES;
gifDate.highlightedTextColor = [UIColor whiteColor];
gifDate.tag = 103;
gifDate.backgroundColor = [UIColor clearColor];
gifDate.extendBottomToFit = NO;
gifDateString = [NSMutableAttributedString attributedStringWithString:[NSString stringWithFormat:#"\ue003 %#",post[#"date"]]];
[gifDateString setFont:[UIFont fontWithName:#"Helvetica Neue" size:12.0]];
[gifDateString setFont:[UIFont fontWithName:#"icomoon" size:10.0] range:NSMakeRange(0,1)];
[gifDateString setTextColor:[UIColor whiteColor]];
[gifDateString setTextAlignment:kCTTextAlignmentRight lineBreakMode:kCTLineBreakByTruncatingTail];
[OHASBasicMarkupParser processMarkupInAttributedString:gifDateString];
gifDate.attributedText = gifDateString;
if (!(post[#"latitude"] == (id)[NSNull null])){
//Location of the Post
gifLocation = [[OHAttributedLabel alloc] initWithFrame:CGRectMake(165,27,141,21)];
gifLocation.autoresizingMask = UIViewAutoresizingNone;
gifLocation.centerVertically = YES;
gifLocation.highlightedTextColor = [UIColor whiteColor];
gifLocation.tag = 107;
gifLocation.backgroundColor = [UIColor clearColor];
gifLocation.extendBottomToFit = NO;
gifLocationString = [NSMutableAttributedString attributedStringWithString:[NSString stringWithFormat:#"\uf041 %#",post[#"locationName"]]];
[gifLocationString setFont:[UIFont fontWithName:#"Helvetica Neue" size:12.0]];
[gifLocationString setFont:[UIFont fontWithName:#"icomoon" size:12.0] range:NSMakeRange(0,1)];
[gifLocationString setTextColor:[UIColor whiteColor]];
[gifLocationString setTextAlignment:kCTTextAlignmentRight lineBreakMode:kCTLineBreakByTruncatingTail];
[OHASBasicMarkupParser processMarkupInAttributedString:gifLocationString];
gifLocation.attributedText = gifLocationString;
}
}
else if (indexPath.row == 1) {
cell = [tableView dequeueReusableCellWithIdentifier:simpleTableBodyIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableBodyIdentifier];
}
//Body of GIF
// Add more from here http://www.easyapns.com/iphone-emoji-alerts
gifBody = post[#"body"];
attrLabel = [[OHAttributedLabel alloc] initWithFrame:CGRectMake(10,5,kLabelWidth,tableView.rowHeight-2*kLabelVMargin)];
attrLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
attrLabel.centerVertically = NO;
attrLabel.automaticallyAddLinksForType = NSTextCheckingAllTypes;
attrLabel.delegate = self;
attrLabel.highlightedTextColor = UIColorFromRGB(0x333333);
attrLabel.tag = kAttributedLabelTag;
attrLabel.backgroundColor = [UIColor clearColor];
attrLabel.extendBottomToFit = YES;
[cell.contentView addSubview:attrLabel];
attrLabel = (OHAttributedLabel*)[cell viewWithTag:kAttributedLabelTag];
mas = [NSMutableAttributedString attributedStringWithString:gifBody];
[mas setFont:[UIFont fontWithName:#"Helvetica Neue" size:14.0]];
[mas setTextColor:UIColorFromRGB(0x333333)];
[mas setTextAlignment:kCTTextAlignmentLeft lineBreakMode:kCTLineBreakByWordWrapping];
[OHASBasicMarkupParser processMarkupInAttributedString:mas];
attrLabel.attributedText = mas;
}
else if (indexPath.row == 2) {
cell = [tableView dequeueReusableCellWithIdentifier:simpleTableActionsIdentifier];
if (cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableActionsIdentifier];
}
[self customizeButtons:btn1];
btn1.tag = (indexPath.section +1)*200;
btn1.titleLabel.font = [UIFont fontWithName:#"icomoon" size:16.0];
if ([post[#"is_Favoring"] boolValue]) {
[btn1 setTitleColor:UIColorFromRGB(0xE4717A) forState:UIControlStateNormal];
}else{
[btn1 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
}
[btn1 setTitle:#"\uf004" forState:UIControlStateNormal];
btn1.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[btn1 addTarget:self
action:#selector(favorAction:)
forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:btn1];
[self customizeButtons:btn2];
btn2.tag = (indexPath.section +1)*2000;
btn2.titleLabel.font = [UIFont fontWithName:#"icomoon" size:16.0];
[btn2 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
[btn2 setTitle:#"\ue000" forState:UIControlStateNormal];
[cell.contentView addSubview:btn2];
btn2.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[btn2 addTarget:self
action:#selector(commentAction:)
forControlEvents:UIControlEventTouchUpInside];
[self customizeButtons:btn3];
btn3.titleLabel.font = [UIFont fontWithName:#"icomoon" size:16.0];
[btn3 setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
[btn3 setTitle:#"\ue001" forState:UIControlStateNormal];
btn3.tag = (indexPath.section +1)*20000;
[cell.contentView addSubview:btn3];
btn3.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[btn3 addTarget:self
action:#selector(otherAction:)
forControlEvents:UIControlEventTouchUpInside];
}
//Paginiation
if (indexPath.section == [dataArray count]-4 && indexPath.row == 1) {
[self loadNextPage];
}
return cell;
}
}
The reason you are seeing an image from another cell is that for efficiency's sake, table cells are reused. To get around this problem, set your image to nil after you dequeue the reusable cell in tableView:cellForRowAtIndexPath:.
First, you shouldn't be doing all of that stuff in cellForRowAtIndexPath.
You should subclass UITableViewCell and layout the cell in it's own class.
Cell for row at indexPath should load the cell and populate data. Not layout loads of controls.
Second, once you've done this you can call...
- (void)prepareForReuse
{
self.imageView.image = nil;
}
This will clear the imageView on dequeue of the cell so that if there is a delay loading hte image then you only see a blank imageView rather than the image that was in there previously.

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