Setting Text Color on Static UITableViewCell - objective-c

I have static cells and want to change text colour. I've made an outlet for the label. In the implementation i'm using
sine.textColor=[UIColor colorWithRed:191 green:48 blue:48 alpha:1.0];
In this case the text colour shows white. However if I use sine.textColor=[UIColor redColor]; it comes up red as expected. How?
Is there any way to change all text labels in one table view controller with one code (so i wouldn't need an outlet for every cell)?

1) UIColor requires a float from 0-1. (That really bugs me too) You can divide values by 255.0 like:
[UIColor colorWithRed:191.0f/255.0f green:48/255.0f blue:48/255.0f alpha:1.0]
In method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
set cell.textColor = [UIColor yourColor];

The way to do this for a STATIC cell is to create OUTLETS for all your cells, and change each individually... Otherwise you have to do this dynamically.
In your .h file:
#property (weak, nonatomic) IBOutlet UITableViewCell *myCell;
In your .m file
[self.myCell.textLabel setTextColor:[UIColor blackColor]];

For clarification the colour change should look like: cell.textLabel.textColor = [UIColor colorWithRed:191.0f/255.0f green:48.0f/255.0f blue:48.0f/255.0f alpha:1.0]; (from UIColor forward you can put whichever RGB values your heart desires) thanks Tier
However -(UITableViewCell *)tableView:(UITableView) *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; doesn't work (for changing all cells' labels with one code ... maybe it's for DYNAMIC cells , however I'm looking for STATIC cells.

Related

iOS: Dynamic Height For UITableViewCell

I'm using iOS9 XCode7
I need to change the height of cell Dynamically according to labelText Height
I have used:
self.tableView.rowHeight=UITableViewAutomaticDimension;
But it is not working for custom made cell.
sizetoFit is removed in iOS9.
Please suggest something.
Thanks
Give your label constrains relative to the cell, top, bottom, left and right.
Than your cell size will grow with the content height.
also make your label multiline.(by setting Lines property to 0 in attribute inspector)
#pragma mark- Menu table Delegates
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;
}
I'm not quite sure what this sentence means (a screenshot would've helped):
"I need to change the height of cell Dynamically according to labelText Height"
So, you have a UITableViewCell, containing a UILabel, and want each cell in your table to have a height depending on that cell's label ?
Here's the code I use.
In my UITableViewCell class, I'll define a static function to measure, and return the height of my .xib file:
#implementation MikesTableViewCell
...
+(CGSize)preferredSize
{
static NSValue *sizeBox = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// Assumption: The XIB file name matches this UIView subclass name.
NSString* nibName = NSStringFromClass(self);
UINib *nib = [UINib nibWithNibName:nibName bundle:nil];
// Assumption: The XIB file only contains a single root UIView.
UIView *rootView = [[nib instantiateWithOwner:nil options:nil] lastObject];
sizeBox = [NSValue valueWithCGSize:rootView.frame.size];
});
return [sizeBox CGSizeValue];
}
#end
Then, in my UIViewController class, I'll tell my UITableView to use this cell, find out it's height.
#property (nonatomic) float rowHeight;
UINib *cellNib = [UINib nibWithNibName:#"MikesTableViewCell" bundle:nil];
[self.tableView registerNib:cellNib forCellReuseIdentifier:#"CustomerCell"];
rowHeight = [MikesTableViewCell preferredSize].height;
...
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return (float)rowHeight;
}
Again, this might not be exactly what you're looking for, but I hope this helps.
try this.
it is so simple.
set this height in heightForRowAtIndexPath method
float vendorNameHeight = [vendorNameLbl.text heigthWithWidth:width andFont:[UIFont fontWithName:#"Signika-Light" size:21.0]];
then
UILabel*vendorNameLbl=[[UILabel alloc]initWithFrame:CGRectMake( 13, 11, 100, 22)];
vendorNameLbl.numberOfLines = 0;
[vendorNameLbl sizeToFitVertically];

How to make UITableView look like the one in Contacts?

I'm trying to make UITableViewController look like following.
(it has picture and several rows for someone's identify.)
I wonder how to make like following programmatically.
How to adjust UITableViewCell's origin.x and width?
How to add a picture at left top?
Please help me.. I will thank you.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier];
cell.frame = CGRectMake(50, 0, 250, 44) <--- ?????? I think this is wrong way.
return cell;
}
My best bet is that either there are two static UITableViews inside a UIScrollView or that it's some custom subclass UIView set as tableHeaderView and styled to look as on the picture. If I were to implement it I'd go with the second choice.
Make a subclass of UITableView. Give your subclass properties referring to the photo well and the three special row cells.
Override layoutSubviews to call [super layoutSubviews] and then set the frames of the photo well and the three special row cells.
The photo well should not be a cell.

UITableView Center Cell Details

I am trying to sync a UITableView with a UILabel to make sure they show the same data; of course, things will be different in the end, but for testing this is what I need to do.
See the arrow? I want that middle cell (from px44-88) to show the cell.textLabel.text in a UILabel when it is the "middle cell".
I tried using - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath but I was having so many problems I figured I'd come here to ask if anyone has a better way of doing this. I'm not sure if it would make a difference or not but I am using NSFetchedResultsController to populate my UITableView.
UITableView is a subclass of UIScrollView, so probably you can use UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
member to detect which cell is currently in the middle.
I.e. assuming you have a plain table with no sections, and tableView is an outlet for the table, label is an outlet for the label, than this will function in your controller will work:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
int row = (scrollView.contentOffset.y + tableView.frame.size.height / 2) / 44;
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
label.text = cell.textLabel.text;
}
Of course, you need to do some scrolling to make it work ;)
You can use visibleCells method to get layout of visible cells for non-plain table and use it to detect cell in the middle of the table.

Several TableView questions

I am looking to create a similar interface to the iTunes iPhone app search interface. I have a few questions I can't seem to find an answer for:
Here is an image of the iTunes iPhone app search interface in question:
How do I create the tableView section subtitle, e.g. the Tap to Preview, Double-Tap to View info under the Top Hits section title.
How is the price button created to float right? Is this done natively or using a custom UIView?
Finally how would I differentiate between a single tap and double tap on any of the table view cells?
You will need to create a Custom UITableViewCell, here's a tutorial. for the price button and other visuals of the cell.
As for the Tap to Preview subtitle, you can create a custom header view, see:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
Most likely, you would need to create 2 UILabels for this view, and obviously you would want to match the fonts used here.
An Example
- (UIView *) tableview:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 30)] autorelease];
UILabel * label = [[UILabel alloc] initWithFrame:CGRectZero];
label.text = #"Top Hits";
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:17];
[headerView addSubview:label];
UILabel * label2 = [[UILabel alloc] initWithFrame:CGRectZero];
label2.text = #"Tap to Preview, Blah blah blah";
label2.backgroundColor = [UIColor clearColor];
label2.font = [UIFont fontWithName:#"Apple's Default Font?" size:17];
[headerView addSubview:label2];
/* something like this */
return headerView;
}
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
return 50.0f;
}
Most of your questions are address well in Apple's Table View Programming Guide for iOS. Here are some short answers:
You can make a header for a section be a UIView, in which case you can use multiple UILabels to create titles with subtitles and the like.
This is probably set as the accessoryView of the UITableViewCell, or it may be an entirely custom subclass of UITableViewCell.
For this, you'll need to implement the UITableViewDelegate method - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath. For more on customizing this method (e.g. to differentiate between single and mutliple taps), check out the Managing Selections section of the above tutorial.
1-For the Custom header
you can use the table view datasource method
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
and then you can either create the view programmtically with all the labels
or you can make an seperate view in the xib ,create its instance in your viewcontroller and return that instance in the above datasource method- this approach would be much easier and simpler.
2- You can implement the price button by using a uibutton and setting it an background image
in a custom table view cell. you can hide/unhide this button as per your functionality.
3- I would suggest you to disable the tableview selection and add tap gesture recognizers to the custom table view cell. this would give you more flexibility
Hope this could help you

How can I customize the image displayed in different table view cells?

I'm trying to add thumbnails into cells in UITableView
cell.imageView.image = [UIImage imageNamed: #"TestThumbnail.jpg"];
This is what i did in
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
method
However I want to add different thumbnails to each cell. Is it possible to do that?
If so, can I add name of each image(eg. "thumbnail.png") to .plist and use that to add thumbnails?
Thank you
It's definitely possible to do that. If you know which image you want to appear in each row ahead of time, one approach would be to construct an array of image file names. You could read that array from a property list, specify it directly in the code, download it from a server... whatever you like. Then, use the row property of the index path to select the image for the cell at that row:
// somewhere in your class declaration...
#property (retain, nonatomic) NSArray *filenames;
// somewhere in your initializer...
self.filenames = [NSArray arrayWithObjects:#"one.jpg", #"two.jpg", #"three.jpg", nil];
// in -tableView:cellForRowAtIndexPath:...
cell.imageView.image = [UIImage imageNamed:[self.filenames objectAtIndex:indexPath.row]];