Autoresizing UITableViewCell - ios

I wan to know how can I resize my table view cell, and see all the message
My app is obtaining the messages from an xml on my server, and then it shows them, but if the message is longer than the text field it just display 3 dots
here is my code:
- (NSInteger)tableView:(UITableView *)myTableView numberOfRowsInSection:(NSInteger)section {
return ( messages == nil ) ? 0 : [messages count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 37;
}
- (UITableViewCell *)tableView:(UITableView *)myTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = (UITableViewCell *)[self.messageList dequeueReusableCellWithIdentifier:#"ChatListItem"];
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"ChatListItem" owner:self options:nil];
cell = (UITableViewCell *)[nib objectAtIndex:0];
}
NSDictionary *itemAtIndex = (NSDictionary *)[messages objectAtIndex:indexPath.row];
UILabel *textLabel = (UILabel *)[cell viewWithTag:1];
textLabel.text = [itemAtIndex objectForKey:#"text"];
UILabel *stextLabel = (UILabel *)[cell viewWithTag:3];
stextLabel.text = [itemAtIndex objectForKey:#"text"];
UILabel *userLabel = (UILabel *)[cell viewWithTag:2];
userLabel.text = [itemAtIndex objectForKey:#"user"];
UILabel *suserLabel = (UILabel *)[cell viewWithTag:4];
suserLabel.text = [itemAtIndex objectForKey:#"user"];
if ([usernameText.text isEqualToString:userLabel.text]){
UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"chat1#2x.png"]];
[cell setBackgroundView:img];
[img release];
textLabel.hidden=NO;
stextLabel.hidden=YES;
userLabel.hidden=NO;
suserLabel.hidden = YES;
}
else{
UIImageView* img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"chat2#2x.png"]];
[cell setBackgroundView:img];
[img release];
textLabel.hidden=YES;
stextLabel.hidden=NO;
userLabel.hidden=YES;
suserLabel.hidden = NO;
}
return cell;
}
Thanks

You have to pass by heightForRowAtIndexPath like this logic (isn't a code ready):
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *itemAtIndex = (NSDictionary *)[messages objectAtIndex:indexPath.row];
NSString *text = [itemAtIndex objectForKey:#"text"];
if(text is larger then what ever you want) {
return yourSpecificCellHeight;
}
return 37;
}
To calculate the size of text see this post on stackOverflow

Related

how to Change the custom tableview cell height and a textview height according to its content in textview

i have created a custom cell with a textview. I need to expand the height of the textview and the height of the custom cell according to the content in the textview without using auto layout. can any one help me
This is the code:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return sendername.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier1 = #"MenuCtrl";
tsecureMsgConCell *cell = [tabVieConv dequeueReusableCellWithIdentifier:CellIdentifier1];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"MsgConCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.userName.text = [sendername objectAtIndex:indexPath.row];
cell.msgContentTxView.text = [msgContents objectAtIndex:indexPath.row];
NSString * sentDateCell = [msgSenddate objectAtIndex:indexPath.row];
NSString * replyDateCell = [msgRevidate objectAtIndex:indexPath.row];
thredIdCell = [TSecureMsgId objectAtIndex:indexPath.row];
if ([sentDateCell isEqualToString:#"NULL"])
{
cell.dateLbl.text = [msgRevidate objectAtIndex:indexPath.row];
cell.timeLbl.text = [msgRecTime objectAtIndex:indexPath.row];
cell.imgVew.image = [UIImage imageNamed:#"recive.png"];
}
else if ([replyDateCell isEqualToString:#"NULL"])
{
cell.dateLbl.text = [msgSenddate objectAtIndex:indexPath.row];
cell.timeLbl.text = [msgSendTime objectAtIndex:indexPath.row];
cell.imgVew.image = [UIImage imageNamed:#"sent.png"];
}
return cell;
}
msgContentTxView - is my textview
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = indexPath.row;
if (row != NSNotFound)
{
if ([exapmlestring length] > 0)
{
CGFloat padding = 17.0f;
CGFloat labelWidth = self.tableview.bounds.size.width - padding*2;
NSDictionary *attributesDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:#"HelveticaNeue" size:17.0], NSFontAttributeName,
nil];
NSAttributedString *text = [[NSAttributedString alloc] initWithString:exapmlestring attributes:attributesDictionary];
NSStringDrawingOptions options = NSStringDrawingUsesLineFragmentOrigin;
///size
CGRect boundingRect = [text boundingRectWithSize:CGSizeMake(labelWidth, CGFLOAT_MAX)
options:options
context:nil];
boundingRect.size.height = boundingRect.size.height + 100;
return (CGFloat) (ceil(boundingRect.size.height) + padding*2);
}
}
return 0;
}

iOS how to display dictionary keys into table cells

I have a dictionary declared as "res". This "res" contains response of JSON data.
I did a valueforkey = "id" to retrieve out the list of id.
When I want to display each id into the tableview it turns out it only displays the same id repetively.
Here is my tableview:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [res count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableViews dequeueReusableCellWithIdentifier:#"cell"];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.textLabel.textColor =[UIColor blackColor];
cell.textLabel.font = [UIFont systemFontOfSize:17.0];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
for(int i = 0; i<res.count; i++)
cell.textLabel.text = [NSString stringWithFormat:#"ID: %#",[res valueForKey:#"id"]];
return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
UIView *view = [[UIView alloc] init];
return view;
}
When i NSLOG out the res with value "id" :
icon = [res valueForKey:#"id"];
NSLog(#"icon: %#", icon);
it shows:
icon: (
300122,
300228,
300235,
300272,
300265,
300242,
300198,
300135,
300282,
300255,
300245,
300248,
300185,
300118,
300275,
300295,
300005,
300062,
300068,
300055,
300095,
300008,
300018,
300015,
300058,
300012,
300085,
300038,
300105,
300002,
300072,
300022,
300025,
300028,
300035,
300258,
300088,
300262,
300128,
300215,
300142,
300078,
300205,
300315,
300238,
300302,
300232,
300285,
300132,
300102
)
Question is how do i display each id on each cell in the tableview?
Set an array of allKeys
NSArray *array = [NSArray arrayWithArray:[yourDictionary allKeys]];'
then just set your tableView dataSource using this array.
e.g
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableViews dequeueReusableCellWithIdentifier:#"cell"];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.textLabel.textColor =[UIColor blackColor];
cell.textLabel.font = [UIFont systemFontOfSize:17.0];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = [NSString stringWithFormat:#"ID: %#",[array objectAtIndex:indexPath.row]];
return cell;
}
Replace numberOfRowsInSection with:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSArray *arrIds = (NSArray *)[res valueForKey:#"id"];
return arrIds.count;
}
And replace
for(int i = 0; i<res.count; i++)
cell.textLabel.text = [NSString stringWithFormat:#"ID: %#",[res valueForKey:#"id"]];
with
NSArray *arrIds = (NSArray *)[res valueForKey:#"id"];
cell.textLabel.text = [NSString stringWithFormat:#"ID: %#",[arrIds objectAtIndex:indexPath.row]];
Store your #"id" values in one array as follows;
NSArray *icon = [res valueForKey:#"id"];
NSLog(#"icon: %#", icon);
Then use that array to display values in TableView cell as follows;
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableViews dequeueReusableCellWithIdentifier:#"cell"];
cell.textLabel.text = [NSString stringWithFormat:#"ID: %#",[icon objectAtIndex:indexPath.row]];
return cell;
}
Replace line below
for(int i = 0; i<res.count; i++)
cell.textLabel.text = [NSString stringWithFormat:#"ID: %#",[res valueForKey:#"id"]];
with line
cell.textLabel.text = [NSString stringWithFormat:#"ID: %#",[[res valueForKey:#"id"] objectAtIndex:indexPath.row]];
your for loop logic is not correct to achieve the goal. We have tableView's default indexPath object to fetch current row in tableview, so fetch the value from your at the tableview's indexPath from your array.
in
ViewDidload{
NSArray * iconArray = [res allKeys];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableViews dequeueReusableCellWithIdentifier:#"cell"];
cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.textLabel.numberOfLines = 0;
cell.textLabel.textColor =[UIColor blackColor];
cell.textLabel.font = [UIFont systemFontOfSize:17.0];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = [NSString stringWithFormat:#"ID: %#",[iconArray objectAtIndex:indexPath.row]];
return cell;
}

Loading thumbnail image in tableView

I have a UITableView within a UIViewController and I'm trying to load the imageFile as a thumbnail in my UITableViewCell. All UITableViewCell data is being loaded from parse and i can successfully load the NSObject name and username into the UITableViewCell but for some reason im getting an error with the thumbnial image. my code is as follows (for cell).
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return self.groups.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *identifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell" forIndexPath:indexPath];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
PFObject *group = [self.groups objectAtIndex:indexPath.row];
PFFile *thumbnail = [group objectForKey:#"imageFile"];
PFImageView *thumbnailImageView = (PFImageView*)[cell viewWithTag:100];
thumbnailImageView.layer.cornerRadius = thumbnailImageView.frame.size.width/2;
thumbnailImageView.layer.masksToBounds = YES;
thumbnailImageView.image = [UIImage imageNamed:#"cc"];
thumbnailImageView.file = thumbnail;
[thumbnailImageView loadInBackground];
UILabel *nameLabel = (UILabel*) [cell viewWithTag:101];
nameLabel.text = [group objectForKey:#"name"];
UILabel *usernameLabel = (UILabel*) [cell viewWithTag:103];
usernameLabel.text = [group objectForKey:#"creatorName"];
return cell;
}
It seems that [cell viewWithTag:100] is a UIImageView in storyboard. Try to set the UIImageView class to PFImageView in identity inspector, hope this would solved your problem too.
Good luck
You are assigning object to PFFile:
The correct usage of PFFile is:
PFFile *thumbnail = [PFFile fileWithName:fileName data:fileData];
The correct usage of PFObject is:
PFObject *group = [PFObject objectWithClassName:#"your class name"];

iOS: Section overlaps UITableView record

I am trying to display three sections (#"Accounts and Tasks", #"Life Events", #"More") and relevant content in an UITableView. I am creating a custom UILabel for each row and try to display text.
Here is my code below. My issue is, The third section instead of displaying third section contents "Social Media", #"About Hop, #"Settingsā€, it is showing some first section contents and overlapping the text.
Could someone guide me to solve this please?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
NSArray *arrTemp1 = [[NSArray alloc]initWithObjects:#"My Accounts",#"Transfers/Deposit",#"Pay Bills", #"Investment Center",#"Claims Center",#"Locators",#"Loan Calculator", nil];
NSArray *arrTemp2 = [[NSArray alloc]initWithObjects:#"Auto Circle",#"Home Circle",nil];
NSArray *arrTemp3 = [[NSArray alloc]initWithObjects:#"Social Media",#"About Hop", #"Settings", nil];
self.keys = [NSArray arrayWithObjects:#"Accounts and Tasks", #"Life Events", #"More", nil];
NSArray *values = [NSArray arrayWithObjects:arrTemp1, arrTemp2, arrTemp3, nil];
self.tableContents = [NSDictionary dictionaryWithObjects:values forKeys:keys];
NSLog(#"table %#",self.tableContents);
NSLog(#"table with Keys %#",[self.tableContents allKeys]);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.keys count];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [self.keys objectAtIndex:section];
}
- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section {
NSArray *listData =[self.tableContents objectForKey:[self.keys objectAtIndex:section]];
return [listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
NSArray *listData =[self.tableContents objectForKey:[self.keys objectAtIndex:[indexPath section]]];
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:SimpleTableIdentifier];
if(cell == nil) {
NSLog(#"listData: %#", listData);
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(26, 4, 250, 30)];
label.text = [listData objectAtIndex:[indexPath row]];
label.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:label];
}
[cell setUserInteractionEnabled:YES];
UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
imv.image=[UIImage imageNamed:#"SettingsMore"];
[cell.contentView addSubview:imv];
return cell;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
....
UILabel *label;
if(cell == nil) {
label = [[UILabel alloc] initWithFrame:CGRectMake(26, 4, 250, 30)];
label.tag = 8888;
label.backgroundColor = [UIColor clearColor];
[cell.contentView addSubview:label];
}
label = (UILabel *)[cell.contentView viewWithTag:8888];
label.text = [listData objectAtIndex:[indexPath row]];
}
If you set label.text in if(cell == nil), the text will not get updated on reusing the cell.

UIImageView in UITableViewCell repeat

I have a bug.
I want to display a UIImageView on cells at special indexPath.row, but these UIImageView repeat while I scroll.
Exemple: I display my UIImageView on a cell indexPath.row == 0, if I scroll down, I see my UIImageView on the cell at indexPath.row == 8.
Here is my code:
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [self getCellContentView:CellIdentifier];
}
UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];
UIImageView *imgRead = (UIImageView *)[cell viewWithTag:6];
[cell.contentView insertSubview:imgRead aboveSubview:lblTemp1];
contentDictio = [dict objectAtIndex:indexPath.row];
lblTemp1.text = [contentDictio objectForKey:#"title"];
NSArray *paths_id = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath_id = ([paths_id count] > 0) ? [paths_id objectAtIndex:0] : nil;
NSString *path_id = [basePath_id stringByAppendingPathComponent:#"id.plist"];
NSMutableArray *mut_array_id = [[NSArray arrayWithContentsOfFile:path_id] mutableCopy];
NSMutableDictionary *c0 = [[NSMutableDictionary alloc] init];
NSString *idPlistData = [contentDictio objectForKey:#"id"];
for(c0 in mut_array_id) {
if([[c0 objectForKey:#"id"] isEqualToString:idPlistData]) {
[imgRead setImage:[UIImage imageNamed:#"read"]];
}
else {
}
}
}
return cell;
}
- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {
CGRect Label1Frame = CGRectMake(kTableCellSmallMargin*2 + 60, kTableCellSmallMargin, 240, 25);
UILabel *lblTemp;
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:cellIdentifier] autorelease];
//Initialize Label with tag 1.
lblTemp = [[UILabel alloc] initWithFrame:Label1Frame];
lblTemp.tag = 1;
lblTemp.backgroundColor = [UIColor clearColor];
[lblTemp setFont: [UIFont fontWithName:#"HelveticaNeue-CondensedBold" size:15.0]];
[cell.contentView addSubview:lblTemp];
[lblTemp release];
UIImageView *read=[[UIImageView alloc] initWithFrame:CGRectMake(kTableCellSmallMargin, kTableCellSmallMargin, 60, 60)];
read.backgroundColor=[UIColor clearColor];
read.tag = 6;
[cell.contentView addSubview:read];
[read release];
return cell;
}
Thanks...
The cell is cached, so you have to clear it when you want no image, like this:
BOOL found = NO;
for(c0 in mut_array_id) {
if([[c0 objectForKey:#"id"] isEqualToString:idPlistData]) {
[imgRead setImage:[UIImage imageNamed:#"read"]];
found = YES;
}
else {
}
}
if (!found)
{
[imgRead setImage:nil];
}

Resources