UITableView textlabel frame not showing proper - ios

I developed iPhone app in which i have UITableView.
UITableViewCell textlabel frame is not proper according to text,
when i write this code, UITableView textlabel is not proper.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CategCellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CategCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CategCellIdentifier];
}
cell.textLabel.backgroundColor=[UIColor redColor];
cell.textLabel.font= [UIFont systemFontOfSize:12.0];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.text = [tempArr objectAtIndex:indexPath.row];
return cell;
}
when i pass my array to UITableView it shows like this:
Whats the problem ? where i am doing mistake, please help

I am not sure what text your array contains. But, I have created an example of my own. Here is the code :
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 10;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CategCellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CategCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CategCellIdentifier];
}
cell.textLabel.backgroundColor = [UIColor redColor];
cell.textLabel.font= [UIFont systemFontOfSize:20.0];
cell.textLabel.textAlignment = NSTextAlignmentCenter;
cell.textLabel.text = #"Brand";
return cell;
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
cell.textLabel.backgroundColor = [UIColor redColor];
}
#end
Here is the output :
I have used your code. Only difference I made is, I am applying red colour to text label in willDisplayCell: method of UITableViewDelegate.
Coming to the margins that appears on both sides of the tableview are as per Apple's UI guidelines.
Let me know if you need my code.

Try to set Size to fit with content After setting Text.
[cell.textLabel sizeToFit];
Thanks.

Check table row height property in size inspector, may be some thing wrong with it
[tableView setRowHeight: 100.00];
or you can use delegate to set row height
(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

Related

IOS: UITableView Jerks When Scrolling Up

I am having this weird issue where my UITableView starts jerking or stuttering when scrolling up, but as soon as I scroll up all the way, it is fine. I am using this table view to hold a chat conversation. I am using a custom cell, and it is only populating text on two labels.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MessagesInfo" forIndexPath:indexPath];
//
// Configure the cell...
WinMessagesInfoCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"MessagesInfo" forIndexPath:indexPath];
if (cell == nil) {
cell = [[WinMessagesInfoCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MessagesInfo"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSNumber *userId = [defaults valueForKey:#"loggedInUserId"];
NSLog(#"Line 791 WinMessageViewController");
if ([self.sender_id objectAtIndex:indexPath.row])
{
if ([[self.sender_id objectAtIndex:indexPath.row] isEqual:userId]){
cell.friendName.textColor = [UIColor blueColor];
cell.friendName.text = #"Me";
}
else
{
cell.friendName.textColor = [UIColor greenColor];
cell.friendName.text = [self.from objectAtIndex:indexPath.row];
}
cell.friendMessage.text = [self.message objectAtIndex:indexPath.row];
}
return cell;
}
Update:
This issue appears to be the height of my cells, if I take out the dynamic cell growth, the issue goes away, but I need them to grow dynamically. I am currently using the following:
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
//minimum size of your cell, it should be single line of label if you are not clear min. then return UITableViewAutomaticDimension;
return UITableViewAutomaticDimension;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension;
}
The estimates must be bad. Any advice to get a better estimate? There are two labels in the cell and only one of the labels will change in size.

UITableViewCell edit cell content on click

I have a question about UITAbleViewCell's.
I have implemented UITableViewDelegate method
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
cell.backgroundColor = [UIColor redColor];
cell.textLabel.textColor = [UIColor redColor];
cell.textLabel.text = #"Title";
}
After I click on desired cell, nothing happens...
Why it doesn't work as I expected? Also, what should I do to make it work?
You have to create some base model for cell states e.g:
#property NSString *modelState = #"red"; // this is fast hint, but it can be a enum with states.
all cell will have one title after tap.
... other controller code...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [self.restaurantTable dequeueReusableCellWithIdentifier:#"cell_ID"];
// cell customization method
[self customizeCell:cell accordingToStateStr:modelState];
return cell;
}
... other controller code...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
// Set other state for cell
self.modelState = #"red";
[tableView reloadData];
}
- (void)customizeCell:(UITableViewCell*)cell accordingToStateStr:(NSString *)str {
if ([str isEqualToString:#"red"]) {
cell.backgroundColor = [UIColor redColor];
cell.textLabel.textColor = [UIColor redColor];
cell.textLabel.text = #"Title";
} else if(...) {
//Other options..
}
}
[tableView reloadData]; - will trigger once again "cellForRow" method and your table will be redrawn according to new model.
You can use for cell states emuns instead NSString object (this is only scaffold for you).
Try this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// config the selected cell
....
}
You should ask the UITableView for the cell directly rather than ask its delegate (self in your code). Cause its delegate may dequeue or create a fresh cell rather than giving you the cell seleceted.

Horizantal tableview cell scrolling iOS

I had an issue in one of the app with horizantal table view scrolling.I created tableview with custom tableviewcell, in the custom tableviewcell I created one more tableview with the tableviewcells and tableview header.I transformed the tableview and tableview cells but after rotation the I am unable to change the frame of table view.Here is the code what I used so far...
- (void)viewDidLoad {
[super viewDidLoad];
//self.ibTableView.estimatedRowHeight = 568.0f;
self.ibTableView.transform=CGAffineTransformMakeRotation(-M_PI/2);
self.ibTableView.backgroundColor=[UIColor clearColor];
self.ibTableView.pagingEnabled=YES;
self.ibTableView.showsVerticalScrollIndicator=NO;
// Do any additional setup after loading the view.}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
SPCustomTableVIewCellTableViewCell *cell = (SPCustomTableVIewCellTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) {
cell = (SPCustomTableVIewCellTableViewCell *)[SPCustomTableVIewCellTableViewCell loadFromNib];
}
[tableView dequeueReusableCellWithIdentifier:#"cell"];
cell.transform=CGAffineTransformMakeRotation(M_PI/2);
cell.backgroundColor = [UIColor clearColor];
return cell;}
SPCustomTableVIewCell.m
- (void)awakeFromNib {
self.headerView = [HeaderTableView loadFromNib];
self.internalTableView.tableHeaderView = self.headerView;
// Initialization code}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
InternalCustomTableViewCell *cell = (InternalCustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) {
cell = (InternalCustomTableViewCell *)[InternalCustomTableViewCell loadFromNib];
[tableView dequeueReusableCellWithIdentifier:#"cell"];
}
cell.backgroundColor = [UIColor clearColor];
return cell; }
Please any help to resolve the issue to set the frame after transformation of table view
If you want to apply animations to a UITableViewCell, you should use
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
I think it is a bad solution to do this in cellForRowAtIndexPath.

TableView Cell contents not shifting in editing mode iOS 6

I have subclass of UITableViewCell. Here is my setup
I have used Autolayout. Now when user swipe on cell , I can see "Delete" button , but it overlaps my right side imageview. Cell contents are not shifting. Here is my code
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.recentItemsArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"ProductCell";
ProductCell *cell = (ProductCell*)[tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil)
{
NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed:#"ProductCell" owner:self options:nil];
cell=[cellArray objectAtIndex:0];
tableView.separatorColor = [UIColor clearColor];
}
Product *product = [self.recentItemsArray objectAtIndex:indexPath.row];
cell.lblProductName.text = product.name;
cell.lblNoOfBids.text = [NSString stringWithFormat:#"%#",product.numberOfBids];
cell.lblBuyItNowPrice.text = [NSString stringWithFormat:#"%# %#",CURRENCY_TYPE,product.buyItNowPrice];
cell.lblTimeRemaining.text = product.timeRemaining;
cell.lblLatestBid.text = [NSString stringWithFormat:#"%# %#",CURRENCY_TYPE,product.currentBidPrice];
cell.lblUserName.text = product.sellerName;
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 110.0f;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
//add code here for when you hit delete
[CommonUtils showAlertWithTitle:#"Alert" andMessage:#"Do you want to delete this product?"];
}
}
I saw several question on Stackoverflow , but didn't get any solution.
Can any body tell me what I am doing wrong ? Any kind of help is highly appreciated. Thanks
Update::
I tried adding UIImageView programatically. still image views are not shifting
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UIImageView* imageView = [[UIImageView alloc]initWithFrame:CGRectMake(260, 0, 50, 60)];
imageView.backgroundColor = [UIColor yellowColor];
[cell.contentView addSubview:imageView];
UIImageView* imageView1 = [[UIImageView alloc]initWithFrame:CGRectMake(20.0, 0, 60, 60)];
imageView1.backgroundColor = [UIColor redColor];
[cell.contentView addSubview:imageView1];
}
//cell.textLabel.text = #"ABC";
// Configure the cell...
return cell;
}
It happend with me. Verify if some cell property have the same name from other property in the view where the UITableView is.

ios - how do I detect if and which item was tapped in a list of items in a UITableView

I have a list of text items which populate a list. The code for it looks like this:
// CREATING EACH CELL IN THE LIST
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"business";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(!cell){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.textLabel.font = [UIFont fontWithName:#"Helvetica" size:17];
cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
}
cell.textLabel.text = [cellTitleArray objectAtIndex:indexPath.row];
// CLOSE THE SPINNER
[spinner stopAnimating];
// return the cell for the table view
return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UIFont *cellFont = [UIFont fontWithName:#"Helvetica" size:17];
CGSize constraintSize = CGSizeMake(self.itemList.frame.size.width, MAXFLOAT);
CGSize labelSize = [[cellTitleArray objectAtIndex:indexPath.row] sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:UILineBreakModeWordWrap];
return labelSize.height + 30;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
What I want to do is to give the person a choice to edit each item. One way it makes sense to do that is by making it a 2-column list and on the right side column to make a character like ">" so they can tap it and it will give them an option to edit the item.
How can that be done in this case?
Thanks!
To capture a row click:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Use indexPath.row to find your selection
...
}
If you watch to capture a click on just a button, or similar:
[self.buttono addTarget:self action:#selector(doAction) forControlEvents:UIControlEventTouchUpInside];
If you wish to use the accessoryType, you would place this code in your cellForRowAtIndexPath method.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// Pick an indicator to suit your need/look
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
Then you action something on the click like so
- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath {
// Perform action for this click on row indexPath.row
}
If you indexPath.row isn't enough to differentiate between your clicks (I can't see why it wouldn't be!) then you can always use setTag on your disclosure button.
[self.tableView indexPathForSelectedRow];
also
[self.tableView indexPathForCell:sender]; //if you have a sender

Resources