I am trying to display multiple beacons in a tableView. The minor values, name, and images for each beacon are stored in core data. While the name and image of the beacon are appearing in the tableView cell correctly, the proximity always changing to the closest beacon.
I really appreciate the help!
DisplayTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"CellID"];
Tag *tag = [self.tags objectAtIndex:indexPath.row];
cell.textLabel.text = #"";
NSData *data = tag.image;
UIImage *image = [UIImage imageWithData:data];
cell.imageView.image = image; //BEACONS
if (self.tags.count > 0)
{
self.arrow.hidden = true;
self.addLabel.hidden = true;
CLBeacon *beacon = [self.beacons objectAtIndex:indexPath.row];
NSString *proximityLabel = #"";
switch (beacon.proximity)
{
case CLProximityFar:
proximityLabel = [NSString stringWithFormat:#"Your %# is Far", tag.name];
cell.backgroundColor = [UIColor colorWithRed:(255/255.0) green:(107/255.0) blue:(105/255.0) alpha:1];
break;
case CLProximityNear:
proximityLabel = [NSString stringWithFormat:#"Your %# is Near", tag.name];
cell.backgroundColor = [UIColor colorWithRed:(96/255.0) green:(102/255.0) blue:(232/255.0) alpha:1];
break;
case CLProximityImmediate:
proximityLabel = [NSString stringWithFormat:#"Your %# is Close", tag.name];
cell.backgroundColor = [UIColor colorWithRed:(118/255.0) green:(225/255.0) blue:(167/255.0) alpha:1];
break;
case CLProximityUnknown:
proximityLabel = #"Fetching Location";
cell.backgroundColor = [UIColor whiteColor];
break;
}
cell.nameLabel.text = tag.name;
NSString *detailLabel = [NSString stringWithFormat:#"%#, Dist: %0.001f", proximityLabel, beacon.accuracy];
cell.proxLabel.text = detailLabel;
self.tableView.hidden = false;
self.mapView.hidden = false;
}
return cell;
}
Related
My tableView freezes while reloading with rows more than 600. is there any solution for that.I am getting data in background thread and try to reload my table in main thread but still the issue is same.
this is my code in cellForRow..
if (![currentUID isEqualToString:[chatDict valueForKey:#"sender"]]) {
identifier = #"onlyRightChat";
}else{
identifier = #"onlyLeftChat";
}
UITableViewCell *cell ;
cell= [tableView dequeueReusableCellWithIdentifier:identifier ];
NSString *userName;
if ([chatDict valueForKey:#"sender_last_name"]) {
userName = [NSString stringWithFormat:#"%# %#",[chatDict valueForKey:#"sender_first_name"],[chatDict valueForKey:#"sender_last_name"]];
}else{
userName = [NSString stringWithFormat:#"%#",[chatDict valueForKey:#"sender_first_name"]];
}
NSString *str ;
if (![currentUID isEqualToString:[chatDict valueForKey:#"sender"]]) {
str = [NSString stringWithFormat:#"%#:\n%#",userName,[[Utils sharedObject] convertUnicodeToEmoji:[chatDict valueForKey:#"text"]]];
}else{
str = [NSString stringWithFormat:#"You:\n%#",[[Utils sharedObject] convertUnicodeToEmoji:[chatDict valueForKey:#"text"]]];
}
CGRect labelSize =[self getRectForString:str];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
TTTAttributedLabel *lbl = (TTTAttributedLabel *)[cell.contentView viewWithTag:5111];
UIView *lblBgView = (UIView *)[cell.contentView viewWithTag:5113];
UIView *bgView = (UIView *)[cell.contentView viewWithTag:5115];
UILabel *timeLbl = (UILabel *)[cell.contentView viewWithTag:5114];
UIImageView *userImgView = (UIImageView *)[cell.contentView viewWithTag:5116];
UILabel *onlineIcon = (UILabel *)[cell.contentView viewWithTag:onlineStatusTag];
onlineIcon.layer.cornerRadius = onlineIcon.frame.size.height/2.0;
onlineIcon.clipsToBounds = YES;
lbl.backgroundColor = [UIColor clearColor];
lbl.enabledTextCheckingTypes = NSTextCheckingTypeLink|NSTextCheckingTypePhoneNumber|NSTextCheckingTypeAddress;
lbl.delegate = self;
if (![currentUID isEqualToString:[chatDict valueForKey:#"sender"]]) {
lbl.linkAttributes = #{NSForegroundColorAttributeName:[UIColor colorWithRed:91/255.0f green:135/255.0f blue:198/255.0f alpha:1.0] ,NSUnderlineStyleAttributeName: #(NSUnderlineStyleSingle)};//[UIColor colorWithRed:119/255.0f green:195/255.0f blue:66/255.0f alpha:1.0]
}else{
lbl.linkAttributes = #{NSForegroundColorAttributeName:[UIColor colorWithRed:194/255.0f green:228/255.0f blue:255/255.0f alpha:1.0] ,NSUnderlineStyleAttributeName: #(NSUnderlineStyleSingle)};//[UIColor colorWithRed:119/255.0f green:195/255.0f blue:66/255.0f alpha:1.0]
}
if ([currentUID isEqualToString:[chatDict valueForKey:#"sender"]]) {
onlineIcon.hidden = YES;
}else{
onlineIcon.hidden = NO;
NSString *status = [chatDict valueForKey:#"status"];
if(status){
if([status isEqualToString:#"online"]){
onlineIcon.backgroundColor = [UIColor colorWithRed:81/255.0 green:217/255.0 blue:93/255.0 alpha:1.0];
}else if([status isEqualToString:#"offline"]){
onlineIcon.backgroundColor = [UIColor yellowColor];
}else{
onlineIcon.backgroundColor = [UIColor colorWithRed:214/255.0 green:103/255.0 blue:101/255.0 alpha:1.0];
}
}else{
onlineIcon.backgroundColor = [UIColor yellowColor];
}
}
userImgView.layer.cornerRadius = userImgView.frame.size.height/2;
userImgView.clipsToBounds = YES;
[userImgView sd_setImageWithURL:[NSURL URLWithString:[chatDict valueForKey:#"sender_picture"]]placeholderImage:[UIImage imageNamed:#"default_avatar"]];
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(userImageViewTap:)];
tapGesture.numberOfTapsRequired = 1;
userImgView.userInteractionEnabled = YES;
[userImgView addGestureRecognizer:tapGesture];
// NSLog(#"cell.frame.size.width %f",cell.frame.size.width);
float width = cell.frame.size.width;
float height = cell.frame.size.height;
if (![currentUID isEqualToString:[chatDict valueForKey:#"sender"]])
{
bgView.frame = CGRectMake(width- labelSize.size.width-31-20-35,0,labelSize.size.width+31+20,cell.frame.size.height);
lblBgView.frame = CGRectMake(0,10,bgView.frame.size.width-15,labelSize.size.height+10);
lbl.frame = CGRectMake(8, 0, lblBgView.frame.size.width-16, labelSize.size.height+10);
timeLbl.frame = CGRectMake(width-170,height-25, 120,fontSize);
timeLbl.textAlignment = NSTextAlignmentRight;
}else{
bgView.frame = CGRectMake(35,0,labelSize.size.width+31+20,cell.frame.size.height);
lblBgView.frame = CGRectMake(15,10,bgView.frame.size.width-15,labelSize.size.height+10);
lbl.frame = CGRectMake(8, 0, lblBgView.frame.size.width-16, labelSize.size.height+10);
timeLbl.frame = CGRectMake(55,height-25, 120,fontSize);
timeLbl.textAlignment = NSTextAlignmentLeft;
}
NSDate* dateComment = [[Utils sharedObject] StringToDate:[chatDict valueForKey:#"created_at"] withFormat:#"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
timeLbl.text = [[Utils sharedObject] relativeTimestamp:dateComment];
timeLbl.font = [UIFont fontWithName:#"Lato-Regular" size:fontSize-3.0];
lblBgView.layer.cornerRadius = 8.0f;
lblBgView.clipsToBounds = YES;
lbl.layer.cornerRadius = 8.0f;
lbl.font = [UIFont fontWithName:#"Lato-Regular" size:fontSize];
lbl.numberOfLines = 0;
lbl.clipsToBounds = YES;
if (str!=(id)[NSNull null])
{
if (![currentUID isEqualToString:[chatDict valueForKey:#"sender"]]) {
lbl.text = str;
}else{
NSDictionary *attrDict = #{ NSFontAttributeName : [UIFont fontWithName:#"Lato-Regular" size:fontSize], NSForegroundColorAttributeName : [UIColor colorWithRed:146/255.0 green:183/255.0 blue:201/255.0 alpha:1.0]};
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:#"You:\n" attributes: attrDict];
NSDictionary *attrDict1 = #{ NSFontAttributeName : [UIFont fontWithName:#"Lato-Regular" size:fontSize]};
NSMutableAttributedString *attrString1 = [[NSMutableAttributedString alloc] initWithString:[[Utils sharedObject] convertUnicodeToEmoji:[chatDict valueForKey:#"text"]] attributes: attrDict1];
[attrString appendAttributedString:attrString1];
lbl.text = str;
lbl.textColor = [UIColor whiteColor];
}
}else{
lbl.text= #"";
}
return cell;
}
and reload call from method..
-(void)getChatTextFromLocalDB{
// [self showLoadingView];
NSString *databaseKey = [NSString stringWithFormat:#"%#-%#",[self.m_dictInfo valueForKey:#"rid"],[[NSUserDefaults standardUserDefaults] objectForKey:#"rid"]];
NSString *localDBPath=[NSHomeDirectory() stringByAppendingPathComponent:#"Documents/GroupChat.plist"];
NSMutableDictionary *root = [[[NSMutableDictionary alloc] initWithContentsOfFile:localDBPath] mutableCopy];
NSMutableArray *chatDictAry = [root objectForKey:databaseKey];
NSString *localDBPath1=[NSHomeDirectory() stringByAppendingPathComponent:#"Documents/GeoConnectChat.plist"];
NSMutableDictionary *root1 = [[[NSMutableDictionary alloc] initWithContentsOfFile:localDBPath1] mutableCopy];
NSMutableArray *temp = [root1 objectForKey:#"chatDeleted"];
NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:#"created_at" ascending:YES];
NSArray *sorters = [[NSArray alloc] initWithObjects:sorter, nil];
NSArray *sortedArray = [chatDictAry sortedArrayUsingDescriptors:sorters];
NSString *lastMsgRid;
[chatDictAry removeAllObjects];
[chatDictAry addObjectsFromArray:sortedArray];
if(chatDictAry.count||[temp containsObject:databaseKey ]){
[self.chatArray removeAllObjects];
for (NSDictionary *dict in chatDictAry) {
if(!([g_Delegate.grpBlockByMeList containsObject:[dict valueForKey:#"sender"]]||[g_Delegate.grpBlockMeList containsObject:[dict valueForKey:#"sender"]])){
[self.chatArray addObject:dict];
if([dict valueForKey:#"rid"])
lastMsgRid = [dict valueForKey:#"rid"] ;
[self.usersArray addObject:[dict valueForKey:#"sender"]];
}
}
[root writeToFile:localDBPath atomically:YES];
dispatch_async(dispatch_get_main_queue(), ^(void){
[self hideLoadingView];
});
if (self.chatArray.count) {
if(tablePositionTop){
CGSize beforeContentSize = self.chatTableView.contentSize;
[self.chatTableView reloadData];
CGSize afterContentSize = self.chatTableView.contentSize;
CGPoint afterContentOffset = self.chatTableView.contentOffset;
CGPoint newContentOffset = CGPointMake(afterContentOffset.x, afterContentOffset.y + afterContentSize.height - beforeContentSize.height);
self.chatTableView.contentOffset = newContentOffset;
}
else{
[self.chatTableView reloadData];
int64_t delayInSeconds = 0.2f;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
if(actualSelf){
[self.chatTableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[self.chatArray count]-1 inSection:0] atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
});
}
[self getUsersOnlineStatus];
if (isFirstTime) {
isFirstTime = NO;
[self getRemainingListFromServerWithLastMsgId:lastMsgRid];
}else{
// [self hideLoadingView];
}
}else{
[self hideLoadingView];
}
}
else
{
if (isFirstTime) {
isFirstTime = NO;
[self showLoadingView];
[self getAllMessages];
}
}
}
I suggest you watch Session 211 of WWDC 2012, Building Concurrent User Interfaces and apply the concepts there. This features cells whose contents are independently queried and rendered.
The basic concept is as follows:
1. In tableView:cellForRowAtIndexPath, a cell is instantiated.
2. In the same method, an operation for retrieving the data to populate the cell is created and stored into a dictionary. A reference to the cell is passed to the operation. The operation has a completion handler that populates the cell and removes the operation from the dictionary.
3. Before the cell is returned from the method, the operation is added to an operation queue.
4. In tableView:didEndDisplayingCell:forRowAtIndexPath, operations for cells that have moved off-screen are cancelled and removed from the dictionary.
I am trying to use two custom cells inside a UITableView. The problem I am running into is that my return is not being called in the right place. I am not sure what I need to implement the return to stop the control from reaching the end of a non-void function.
Here is the code inside my cellforrowatindex.
static NSString *kCellID = #"MessageCell";
static NSString *UCellID = #"UnreadCell";
if (status == 0)
{
//static NSString *kCellID = #"MessageCell";
MessageListCell *cell = (MessageListCell *) [tableView dequeueReusableCellWithIdentifier:kCellID];
if (priority == 0)
{
cell. imageviewImportance.image = nil;
}
if (priority == 1)
{
cell.imageviewImportance.image = [UIImage imageNamed:#"single exclamation.png"];
}
if (priority == 2)
{
cell.imageviewImportance.image = [UIImage imageNamed:#"double exclamation mark.png"];
}
if (priority == 3)
{
cell.imageviewImportance.image = [UIImage imageNamed:#"triple exclamation kmar.png"];
}
//if (status == 0)
//{
cell.backgroundColor = [UIColor clearColor];
cell.labelFrom.text = [NSString stringWithFormat:#"From: %#",m.From];
cell.labelFrom.font = [UIFont boldSystemFontOfSize:17];
cell.labelSubject.text = subject;
cell.labelSubject.font = [UIFont boldSystemFontOfSize:17];
cell.labelTime.text = time;
cell.labelTo.text = [NSString stringWithFormat:#"To: %#",m.To];
cell.labelTo.font = [UIFont boldSystemFontOfSize:17];
return cell;
}
else if (status != 0)
{
//static NSString *UCellID = #"UnreadCell";
MessageListCell *cell = (MessageListCell *)[tableView dequeueReusableCellWithIdentifier:UCellID];
cell.backgroundColor = [UIColor clearColor];
cell.unreadLabelFrom.text = [NSString stringWithFormat:#"From: %#",m.From];
cell.unreadLabelSubject.text = subject;
cell.unreadLabelTo.text = [NSString stringWithFormat:#"To:%#",m.To];
cell.unreadLabelTime.text = time;
return cell;
}
//NSLog(#"description = %#",[cell description]);
//return cell;
}
Simply change the else if to else, then the compiler will stop complaining.
The way you have it now is telling the compiler to check 2 condition, what if none of them are met? what is going to be returned?
You will need a else block but since your else if is redundant so else is what you need.
if (status == 0)
{
static NSString *kCellID = #"MessageCell";
MessageListCell *cell = (MessageListCell *) [tableView dequeueReusableCellWithIdentifier:kCellID];
NSString *imageName;
switch (priority) {
case 0:
imageName = nil;
break;
case 1:
imageName = #"single exclamation.png";
break;
case 2:
imageName = #"double exclamation mark.png";
break;
case 3:
imageName = #"triple exclamation kmar.png";
break;
default:
break;
}
cell.imageviewImportance.image = [UIImage imageNamed:imageName];
cell.backgroundColor = [UIColor clearColor];
cell.labelFrom.text = [NSString stringWithFormat:#"From: %#",m.From];
cell.labelFrom.font = [UIFont boldSystemFontOfSize:17];
cell.labelSubject.text = subject;
cell.labelSubject.font = [UIFont boldSystemFontOfSize:17];
cell.labelTime.text = time;
cell.labelTo.text = [NSString stringWithFormat:#"To: %#",m.To];
cell.labelTo.font = [UIFont boldSystemFontOfSize:17];
return cell;
}
else
{
static NSString *UCellID = #"UnreadCell";
MessageListCell *cell = (MessageListCell *)[tableView dequeueReusableCellWithIdentifier:UCellID];
cell.backgroundColor = [UIColor clearColor];
cell.unreadLabelFrom.text = [NSString stringWithFormat:#"From: %#",m.From];
cell.unreadLabelSubject.text = subject;
cell.unreadLabelTo.text = [NSString stringWithFormat:#"To:%#",m.To];
cell.unreadLabelTime.text = time;
return cell;
}
I am having a really weird error that I cannot figure out, I really need some help right now. Basically, the tableView has three UITableViewCell subclasses that have their own prototype cell, and appear in their own section. But the prototype that is in the third section is also appearing in the second section. The weird part is that after putting some NSLogs into my code, the cell that is showing up in the second section section (which should be in the third section) is the correct subclass of UITableViewCell, but the content from the other prototype is showing up. I have no idea why this is happening. I have check to make sure the prototypes are of the right subclass, and the identifiers are all correct, and they are. Has anyone encountered this problem before? Sorry in advance for how long my cellForRowAtIndexPath: method is:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
WaveDetailCell *waveDetails;
ActionsCell *actions;
CommentCell *comments;
id cellToReturn;
if (self.hasAgrees || self.hasComments)
{
switch (indexPath.section)
{
case 0:
{
waveDetails = [tableView dequeueReusableCellWithIdentifier:WaveDetailCellIdentifier forIndexPath:indexPath];
if ([waveDetails.waveLabel respondsToSelector:#selector(setAttributedText:)])
{
NSString *name = self.currentWaveObject.wavedToUserID;
NSString *fullWaveString = [name stringByAppendingString:self.currentWaveObject.waveString];
NSRange range = [fullWaveString rangeOfString:self.currentWaveObject.wavedToUserID];
UIFont *font = [UIFont fontWithName:HelveticaMedium size:16.0f];
NSDictionary *attrs = #{NSFontAttributeName: font};
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:fullWaveString];
[attributedString addAttributes:attrs range:range];
[waveDetails.waveLabel setAttributedText:attributedString];
}
waveDetails.wavedByNameLabel.text = self.currentWaveObject.wavedByUserID;
waveDetails.timestampLabel.text = self.currentWaveObject.creationDate;
//round the corners of the imageview
[self setCornerRadiusForImageView:waveDetails.profilePicImageView withRadius:CGSizeMake(4.0f, 4.0f)];
[self setCornerRadiusForImageView:waveDetails.waverProfileImageView withRadius:CGSizeMake(2.0f, 2.0f)];
cellToReturn = waveDetails;
}
break;
case 1:
{
if (self.hasAgrees)
{
//create agrees cell
}
else
{
actions = [tableView dequeueReusableCellWithIdentifier:ActionsCellIdentifier forIndexPath:indexPath];
cellToReturn = actions;
}
}
case 2:
{
if (self.hasAgrees)
{
//if there are agrees, and no comments, then the last section should be the actions cell
actions = [tableView dequeueReusableCellWithIdentifier:ActionsCellIdentifier forIndexPath:indexPath];
cellToReturn = actions;
}
else
{
//there are comments, and no agrees
comments = [tableView dequeueReusableCellWithIdentifier:CommentCellIdentifier forIndexPath:indexPath];
CommentObject *comment = [self.currentWaveObject.commentsArray objectAtIndex:indexPath.row];
comments.userNameLabel.text = comment.commenterID;
comments.commentLabel.text = comment.commentText;
comments.timeStampLabel.text = comment.timeStamp;
[self setCornerRadiusForImageView:comments.userImageView withRadius:CGSizeMake(3.0f, 3.0f)];
cellToReturn = comments;
}
}
default:
break;
}
}
else if (self.hasComments && self.hasAgrees)
{
switch (indexPath.section)
{
case 0:
{
waveDetails = (WaveDetailCell *)[tableView dequeueReusableCellWithIdentifier:WaveDetailCellIdentifier forIndexPath:indexPath];
if ([waveDetails.waveLabel respondsToSelector:#selector(setAttributedText:)])
{
NSString *name = self.currentWaveObject.wavedToUserID;
NSString *fullWaveString = [name stringByAppendingString:self.currentWaveObject.waveString];
NSRange range = [fullWaveString rangeOfString:self.currentWaveObject.wavedToUserID];
UIFont *font = [UIFont fontWithName:HelveticaMedium size:16.0f];
NSDictionary *attrs = #{NSFontAttributeName: font};
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:fullWaveString];
[attributedString addAttributes:attrs range:range];
[waveDetails.waveLabel setAttributedText:attributedString];
}
waveDetails.wavedByNameLabel.text = self.currentWaveObject.wavedByUserID;
waveDetails.timestampLabel.text = self.currentWaveObject.creationDate;
//round the corners of the imageview
[self setCornerRadiusForImageView:waveDetails.profilePicImageView withRadius:CGSizeMake(4.0f, 4.0f)];
[self setCornerRadiusForImageView:waveDetails.waverProfileImageView withRadius:CGSizeMake(0.5f, 0.5f)];
cellToReturn = waveDetails;
}
break;
case 1:
{
//create agrees cell
}
break;
case 2:
{
actions = (ActionsCell *)[tableView dequeueReusableCellWithIdentifier:ActionsCellIdentifier forIndexPath:indexPath];
cellToReturn = actions;
}
break;
case 3:
{
comments = (CommentCell *)[tableView dequeueReusableCellWithIdentifier:CommentCellIdentifier forIndexPath:indexPath];
CommentObject *comment = [self.currentWaveObject.commentsArray objectAtIndex:indexPath.row];
comments.userNameLabel.text = comment.commenterID;
comments.commentLabel.text = comment.commentText;
comments.timeStampLabel.text = comment.timeStamp;
[self setCornerRadiusForImageView:comments.userImageView withRadius:CGSizeMake(2.0f, 2.0f)];
cellToReturn = comments;
}
break;
default:
break;
}
}
else
{
if (indexPath.row == 0)
{
waveDetails = (WaveDetailCell *)[tableView dequeueReusableCellWithIdentifier:WaveDetailCellIdentifier forIndexPath:indexPath];
if ([waveDetails.waveLabel respondsToSelector:#selector(setAttributedText:)])
{
NSString *name = self.currentWaveObject.wavedToUserID;
NSString *fullWaveString = [name stringByAppendingString:self.currentWaveObject.waveString];
NSRange range = [fullWaveString rangeOfString:self.currentWaveObject.wavedToUserID];
UIFont *font = [UIFont fontWithName:HelveticaMedium size:16.0f];
NSDictionary *attrs = #{NSFontAttributeName: font};
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:fullWaveString];
[attributedString addAttributes:attrs range:range];
[waveDetails.waveLabel setAttributedText:attributedString];
}
waveDetails.wavedByNameLabel.text = self.currentWaveObject.wavedByUserID;
waveDetails.timestampLabel.text = self.currentWaveObject.creationDate;
//round the corners of the imageviews
[self setCornerRadiusForImageView:waveDetails.profilePicImageView withRadius:CGSizeMake(4.0f, 4.0f)];
[self setCornerRadiusForImageView:waveDetails.waverProfileImageView withRadius:CGSizeMake(0.5f, 0.5f)];
cellToReturn = waveDetails;
}
else
{
actions = (ActionsCell *)[tableView dequeueReusableCellWithIdentifier:ActionsCellIdentifier forIndexPath:indexPath];
cellToReturn = actions;
}
}
return cellToReturn;
}
Wait, I figured it out. This is embarrassing, but I forgot a break in one of the cases in the switch statement!
I am trying to add a UITableView inside my UIAlertView and its not showing up. I just see title & message along with "OK" button on alertview.
Below is my code and I am running on iOS 7.
- (void)showDataReceivedAlert {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Received Data" message:#"\n\n\n\n\n\n\n\n\n\n\n\n" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(10, 40, 264, 120)];
table.delegate = self;
table.dataSource = self;
table.backgroundColor = [UIColor clearColor];
[alert addSubview:table];
[alert show];
}
- (NSInteger)tableView:(UITableView *)iTableView numberOfRowsInSection:(NSInteger)iSection {
return 6;
}
- (UITableViewCell *)tableView:(UITableView *)iTableView cellForRowAtIndexPath:(NSIndexPath *)iIndexPath {
static NSString *identifier = #"iBeaconDataCell";
UITableViewCell *cell = [iTableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryNone;
}
NSString *cellText;
NSString *cellLabelText;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSData *encodedObject = [userDefaults objectForKey:#"data"];
MyObj *myData = (MyObj *)[NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];
switch (iIndexPath.row) {
case 0:
cellText = #"Data 1";
cellLabelText = [NSString stringWithFormat:#"%d", myData.data1];
break;
case 1:
cellText = #"Data 2";
cellLabelText = [NSString stringWithFormat:#"%d", myData.data2];
break;
case 2:
cellText = #"Data 3";
cellLabelText = [NSString stringWithFormat:#"%d", myData.data3];
break;
case 3:
cellText = #"Data 4";
cellLabelText = [NSString stringWithFormat:#"%d", myData.data4];
break;
case 4:
cellText = #"Data 5";
cellLabelText = [NSString stringWithFormat:#"%f", myData.data5];
break;
case 5:
cellText = #"Data 6";
cellLabelText = myData.data6;
break;
default:
break;
}
cell.textLabel.text = cellText;
UILabel *cellLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 150.0, 44.0)];
cellLabel.text = cellLabelText;
cell.accessoryView = cellLabel;
return cell;
}
The default alert isn't meant to take subviews. (while in ios6 and earlier add. subviews could be hacked in, this does NOT work in iOS7)
write your own custom UIView that looks and acts like an alert.
There's a lot of implementations around if you prefer not to do it yourself :) e.g. on cocoacontrols.com
From iOS 7 you need to add like this
[alertview setValue:tableView forKey:#"accessoryView"];
I'm having some problems with an UITableView and the method reloadData.
I'm refreshing the tableView when clicking over a refresh button with the last info in a data base ordered descendingly so the most recent items should appear in first position.
Every cell in my tableview is being filled with custom labels and uiimageviews.
Well... the thing is that when I press that refresh button and new data is found, the first 5 rows (which are the ones that are being shown in the display) aren't updated but the following rows do. However, if I press over any of the 5 first rows, they call correctly to the tableView:didSelectRowAtIndexPath: method with the new data.
So, the problem is that the tableview's "visual" content of the first 5 rows is not being updated.
Anyone can help me with this?
Thanks!
Code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = [ NSString stringWithFormat: #"%d:%d", [ indexPath indexAtPosition: 0 ], [ indexPath indexAtPosition:1 ]];
UITableViewCell *cell = [ tableView dequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame: CGRectZero reuseIdentifier: CellIdentifier] autorelease];
// Configure the cell...
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
Activity *activity = [[self.activities valueForKey:[[[self.activities allKeys] sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)] objectAtIndex:indexPath.section]] objectAtIndex:indexPath.row];
cell.textLabel.text=#"";
//Mostramos el nombre
UILabel *label= [[UILabel alloc] initWithFrame:CGRectMake(60, -5, 200, 34)];
label.text = [activity name];
label.font = [UIFont boldSystemFontOfSize:17];
label.backgroundColor = [UIColor clearColor];
[cell addSubview:label];
[label release];
//Mostramos la imagen
UIImageView *leftImage = [[UIImageView alloc] initWithFrame:CGRectMake(5, 3, 50, 50)];
[cell addSubview:leftImage];
UITextView *textView=[[UITextView alloc] initWithFrame:CGRectMake(60, 25, 200, 38)];
textView.editable = NO;
textView.backgroundColor = [UIColor clearColor];
textView.textColor = [UIColor grayColor];
textView.font = [UIFont systemFontOfSize:12.0];
textView.contentInset = UIEdgeInsetsMake(-8,-8,0,0);
textView.userInteractionEnabled=NO;
[cell addSubview:textView];
switch ([activity notType]) {
case 0:
textView.text = [NSString stringWithFormat:NSLocalizedString(#"requestPUSH",#""),[activity name]];
leftImage.image = [UIImage imageNamed:#"ios.png"];
break;
case 1:
textView.text = [NSString stringWithFormat:NSLocalizedString(#"MPCreatedPushString",#""),[activity name],[activity nameItem]];
leftImage.image = [UIImage imageNamed:#"mpNew.png"];
break;
case 2:
textView.text = [NSString stringWithFormat:NSLocalizedString(#"MPUpdatedPushString",#""),[activity name],[activity nameItem]];
leftImage.image = [UIImage imageNamed:#"mpUpdated.png"];
break;
case 3:
textView.text = [NSString stringWithFormat:NSLocalizedString(#"MPDeletedPushString",#""),[activity name],[activity nameItem]];
leftImage.image = [UIImage imageNamed:#"ios.png"];
break;
case 4:
textView.text = [NSString stringWithFormat:NSLocalizedString(#"MPGuestConfirmationPUSHString",#""),[activity name],[activity nameItem]];
leftImage.image = [UIImage imageNamed:#"attend.png"];
break;
case 5:
if ([[activity message] isEqualToString:#"noData"]) {
textView.text = [NSString stringWithFormat:NSLocalizedString(#"ShoutPushString",#""),[activity name]];
}else{
textView.text = [NSString stringWithFormat:NSLocalizedString(#"ShoutPushStringWithData",#""),[activity name], [activity message]];
}
UIImage *contactImage = [UIImage imageWithData:[[activity person] pic]];
if (contactImage!=nil) {
leftImage.image = contactImage;
//redondeamos bordes
CALayer * l = [leftImage layer];
[l setMasksToBounds:YES];
[l setCornerRadius:5.0];
}else{
leftImage.image = [UIImage imageNamed:#"ios.png"];
}
break;
case 6:
textView.text = [NSString stringWithFormat:NSLocalizedString(#"CheckinPushString",#""),[activity name],[activity nameItem]];
leftImage.image = [UIImage imageNamed:#"ios.png"];
break;
case 7:
textView.text = [NSString stringWithFormat:NSLocalizedString(#"MPGuestRejectionPUSHString",#""),[activity name],[activity nameItem]];
leftImage.image = [UIImage imageNamed:#"reject.png"];
break;
default:
break;
}
[leftImage release];
[textView release];
//Mostrar fecha
double timestamp = [[activity datetime] doubleValue]/1000;
NSString *dateShow;
NSDate *datetime = [NSDate dateWithTimeIntervalSince1970:timestamp];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"HH:mm"];
dateShow=[dateFormat stringFromDate:datetime];
UILabel *label2= [[UILabel alloc] initWithFrame:CGRectMake(240, 0, 70, 20)];
label2.text = dateShow;
label2.textAlignment = UITextAlignmentRight;
label2.font = [UIFont systemFontOfSize:12];
label2.backgroundColor = [UIColor clearColor];
label2.textColor = [UIColor blueColor];
[cell addSubview:label2];
[label2 release];
}
return cell;
}
Hmm, so you give each cell a unique cell identifier. Do you have a reason for that?
Mostly people try to patch their misunderstanding of iOS table views that way ...
When you reload your table this call UITableViewCell *cell = [ tableView dequeueReusableCellWithIdentifier: CellIdentifier];
will get you a cell to reuse (read already contains content).
But your code only works for "fresh" cells : if (cell == nil) {
, you're just not prepared for that case ...