How to add cell count in UILabel in Objective C - objective-c

I am new in iOS and I am facing problem regarding to count cell of UITableView and add it in UILabel.
I need to do it as shown in Image
As in the Image there is count 1,2,3,4 .Did anybody done this.Thanks in Advance!

In your cellForRowAtIndexPath you just set the UILabel of the cell, like so:
cell.countLabel.text = [NSString stringWithFormat:#"%ld)", (long)indexPath.row +1];

Xcode 8.0 in
cell.countLabel.text =[nsstring stringwithformate:#"%d",indexpath.row+1];

Related

How to know the current UITableView cell position with respect to the UIViewController in swift 3.0?

How to know the current UITableView cell position with respect to the UIViewController in swift 3.0? In my case i m using a label on UITableViewCell with UITapGesture as per my requirement in Swift.
This is done easily with convert(_ rect: CGRect, from view: UIView?). See more details here. So based on that, you can try this:
let rect = view.convert(tableView.rectForRow(at: indexPath), from: tableView)
Where view is your viewController's view and tableView is the table view containing the cell.
try this :
YOURCell.convert(YOURCell.frame, to:YOURUIViewController.view)
Do you want to know position (x,y) for cell?
We can get CGRect of cell with indexPath.
CGRect cellRect = [tableView rectForRowAtIndexPath:indexPath];
In Swift you can use the following, assuming tableView is the table view containing the cell that you want to get the position of:
tableView.convert(tableView.rectForRow(at: indexPath), to: self.view)
I have tested this using Swift 4 and iOS 11.3

UILabel render text incorrectly in IOS7

I use following code to calculate the bound of a UILabel
CGRect bound = [lblName.text boundingRectWithSize:(CGSize){206, 99999}
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
attributes:stringAttributes
context:nil];
The UILabel is a embedded in a UIScrollView, which is a subview of UITableViewCell.
here what i got
I made a test which use a UILabel in a table cell, and a UILabel in UIScrollView separately, and results are as I expected
Note that all setting (font, line break mode etc) of UILabel are the same in all those case. The boundingRectWithSize returns same result in all those case, only difference is the way UILabel render the text.
What is the problem here? did i miss sometthing?
UPDATE: this happen only when i load UILabel from nib, if it is created programmatically, there is no problem. (my project is migrated from xcode 4 to xcode 5)
Try this:
bound.size.height += 1;
UPDATE:
According to Apple's document
- (CGRect)boundingRectWithSize:(CGSize)size options:(NSStringDrawingOptions)options attributes:(NSDictionary *)attributes context:(NSStringDrawingContext *)context
This method returns fractional sizes (in the size component of the returned CGRect); to use a returned size to size views, you must use raise its value to the nearest higher integer using the ceil function.
So you might want to use this approach:
bound.size.height = ceil(bound.size.height);
I was seeing the same behavior with some of my labels, which looked fine in iOS 6, but in iOS 7 they had extra padding at the top and bottom as in your pictures.
Here's what I had to do to finally get it to layout correctly in viewDidLoad - works on both iOS 6 and 7.
self.someLabel.autoresizingMask = UIViewAutoresizingNone;
self.someLabel.frame = CGRectMake(
self.someLabel.frame.origin.x,
self.someLabel.frame.origin.y,
labelWidth, // define elsewhere if you're targeting different screen widths
self.someLabel.bounds.size.height);
[self.someLabel sizeToFit];

Removing Custom UITableviewCell border

I have a tableview in my iPhone project. Each cell of the row is customized with a separate UITableViewCell.
The custom cell contains an imageview and a label. My row height is 55 and hence my custom table height is also 55. Imageview lies as a background for the cell, so imageview height is also 55. But when i run the project i could see a border around each row, which seems the custom cell is having a rectangular border around its contents.
How could i be able to remove this bounding rectangle. i am working in xcode 4.2.1 and simulator 5.0.
its possible that the border you see is the seperator, try to add this line to your code
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
Is it the cell separator?
tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
You need to set the background view of cell-
cell.backgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
actually it was due to the image i loaded.. it had a shadow part, which appeared as the white space.. sorry for the trouble. THANK YOU ALL

iphone sdk how to change customcell colour on selecting the cell?

i am using customcell with label. How to change the textcolour of label when the cell selected .By default it is black after selecting that, it needs to be white. How to do that in customcell point. Anyone help me.
Thanks in advance.
Try this code(I presume that you have an access to that label, right?):
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
yourLabel.textColor = //Your color;
}
You have to take one integer. when you select the cell assign the row value to that integer and reload the table view. in cellForRowAtIndexPath method out the code like
if(selectedIndex == indexpath.row){
textLabel.textColor = [UIColor whiteColor];
}
selectedIndex is a integer value
try out I am not tested but it may be working
In the custom cell xib .I have connected the label to the customcell.i just changed the text colour and highlighted text colour in the xib .and it is working great.

What is the best way to implement a variable-sized multi-line UITableCell?

I'm trying to display a table full of twitter statuses (yes, this is the Stanford Presence 2 assignment), which are variably sized. I can relatively easily determine the appropriate height for my rows with code that approximates (from accompanying lecture 9):
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *) indexPath
{
NSString *text = ...;
UIFont *font = [UIFont systemFontOfSize:...];
CGSize withinSize = CGSizeMake(tableView.width, 1000];
CGSize size = [text sizeWithFont:font constrainedToSize:withinSize lineBreakMode:UILineBreakModeWordWrap];
return size.height + somePadding;
}
I have tried two approaches (and some tweaks to both) to get a multi-line word-wrapping field of text into my table row.
Add a UILabel as a subview to my custom UITableCell subclass, and set the numberOfLines property to either a calculated number based on the height above (say, 6), or to 0 (theoretically unlimited). The numberOfLines is ignored; I see either 1 or 2 lines, and no more.
Add a read-only UITextView as a subview. This has the problem that the UITextView eats my scrolling; I end up scrolling inside a UITextView row instead of moving smoothly from row to row. If I disable scrolling on the UITextView, I end up being unable to scroll at all.
This is a pretty common thing to do; what's the best way to accomplish it?
You might want to look at the userInteractionEnabled property of the UITextView. That should allow input to be passed through to the UITableView so you get scrolling.
Here's a link to a blog I posted on this subject. I used a UILabel with numberOfLInes = 0. I hope this will be of some help.
Sample Project with Variable Sized UITableViewCell
While playing with userInteractionEnabled=NO, scrollEnabled=NO, and getting the right autosizing parameters set in IB worked, I think that going with a UILabel with numberOfLines=0 and the same autosizing parameters is ultimately a better idea, for the next person.