I want this below type of layout in iPhone (using UItableview, Uitableviewcell, Uilabel and Uiimageview).
(assume light blue dots == uiimageview)
In each UItableviewcell I have 1 UIimageview and 1 Uilabel.
both control's starting alightment are same TOP LEFT.
My uilabel's content are dynamic and image is only one static image.
Image is very small compare to uilabel's content.
So, my image goes center vertical and horizontally. but I want only on TOP Left corner.
Edited :
I have already do this in Uitableviewcell
But if Uilabel have number of line is 2 or more then imageview's position is changed.
It goes in center vertically and horizontally. but I want imageview only on top left position whether uilabel's number of line 1,2,3....n
my code is here:
In CommonCustomeCell.m
- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier];
if (self) {
dotImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"dotPoint.png"]];
dotImage.opaque = YES;
[self.contentView addSubview:dotImage];
titleName3 = [[UILabel alloc] init];
titleName3.textAlignment = UITextAlignmentLeft;
titleName3.backgroundColor = [UIColor clearColor];
titleName3.adjustsFontSizeToFitWidth = FALSE;
titleName3.font = [UIFont systemFontOfSize:14];
titleName3.textColor = [UIColor colorWithRed:17.0/255.0 green:72.0/255.0 blue:147.0/255.0 alpha:1];
titleName3.lineBreakMode = NSLineBreakByWordWrapping;
[titleName3 sizeToFit];
[self.contentView addSubview:titleName3];
}
-(void)layoutSubviews
{
frame = CGRectMake(20,2,self.bounds.size.width - 45 , self.bounds.size.height);
[titleName3 setFrame:frame];
frame = CGRectMake(4,10,dotImage.frame.size.width,dotImage.frame.size.height);
self.dotImage.frame = frame;
}
}
In RootViewController.m file
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
CommonCustomeCell *cell = (CommonCustomeCell *) [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CommonCustomeCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
[cell.dotImage5 setImage:[UIImage imageNamed:#"myLogo.png"]];
cell.titleName3.text = [[myArr objectAtIndex:indexPath.row] objectForKey:#"title"];
}
}
help me, Thanks in advance.
Create UITableView with Section and grouped style.
Your loarem Ipsm (text) set as a Section Title.
and Create UITableViewCell custom cell with UIImageView and UILabel to display text
"loarem Ipsm is simple dummy"
Have you set autosizing for the CustomCell View same as given in the image below? Only top and left should be enabled.
Related
Hi i am a newbie to iOS.
I have implemented a tableview in ViewController1.In the tableview i am displaying tittle,detailText and disclosure indicator for the cell.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1
reuseIdentifier:MyIdentifier];
}
cell.textLabel.text =[listTableArray objectAtIndex:indexPath.row];
cell.textLabel.textColor=[UIColor grayColor];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.text=#"4mile";
return cell;
}
Everything works fine,Now i need a image before the disclosure indicator when i do it with
UIImageView *accessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
accessoryView.image=[UIImage imageNamed:#"list.png"];
cell.accessoryView = accessoryView;
Disclosure indicator is replaced by the image.But i need both image along with disclosure indicator without using Custom cell.
How can i do it...? Help me out.
Thanks
similiar to the due date but at the place of time i need image
UITableViewCell have a fixed layout, depending on the style you use when you initialize it. You cannot change that layout, since the cell lays out its subviews as it prefers to have them.
add the - (void)layoutSubviews to your cell subclass like this:
- (void)layoutSubviews {
[super layoutSubviews];
self.imageView.frame = CGRectMake(0.0f , 0.0f, 20.0f, 20.0f);
}
else try this
UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = [UIImage imageNamed:#"list.png"];;
[imageView setFrame:CGRectMake(50, 5, 20, 20)]; // customize the frame
[cell.contentView addSubview:imageView];
If you wanna add UIImageView to standard UITableViewCell you need add your imageView to contentView of cell and add all subviews like a textLabel, detailTextLabel and accessoryView
UIImageView *accessoryImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
accessoryImageView.image=[UIImage imageNamed:#"list.png"];
[cell.contentView addSubview:accessoryImageView];
or subclass of UITableViewCell and implement all views inside it
I want to add 2 imageViews on the Left and Right and a cell textLabel on the Center on TableView.
The 2 imageViews has different role and action.
(Being able to touch, hidden and not hidden)
Now the left side imageView is on the top of the center textLabel.
So I want to change the textLabel's width and x position not to be on the top of imageViews.
I tried this code but it didn't work.
CGRect frame = CGRectMake(40, 0, 200, 40);
cell.textLabel.frame = frame;
and I also tried to add UILabel on the tableView.
But I will implement TableViewCell animation and if I add UILabel, it doesn't work.
So I don't use this way.
How can I solve it?
here is the code.
- (void)viewDidLoad {
tableView = [[UITableView alloc]initWithFrame:rect style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
[self.view addSubview: self.tableView];
self.tableView.backgroundColor = [UIColor clearColor];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"Cell"];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
[self updateCell:cell atIndexPath:indexPath];
return cell;
}
- (void)updateCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
NSMutableArray *set = [self.section objectAtIndex:indexPath.row];
cell.backgroundColor = [UIColor blueColor];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.font = [UIFont fontWithName:#"HiraKakuProN-W3" size:15];
cell.textLabel.adjustsFontSizeToFitWidth = YES;
cell.textLabel.minimumScaleFactor = 10.0f;
cell.textLabel.numberOfLines = 0;
}
you need to override the layoutSubviews for your UItableviewCell class
- (void)layoutSubviews {
[super layoutSubviews];
CGSize size = self.bounds.size;
self.textLabel.frame = CGRectMake(2.0f, 4.0f, size.width, size.height); // or customize
self.textLabel.contentMode = UIViewContentModeScaleAspectFit;
}
Choice-2
crate the UILabel and subview to cell.contentView
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(4, 4, 200, 30)];
label.text = #"Test";
[cell.contentView addSubview:label];
Update
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
[self updateCell:cell atIndexPath:indexPath];
if (cell == nil){
myCellView = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"Cell"];
UILabel *orderLbl = [[UILabel alloc] initWithFrame:CGRectMake(1, 13, 176, 21)];
[orderLbl setTag:1];
orderLbl.textColor = [UIColor whiteColor];
[orderLbl setBackgroundColor:[UIColor clearColor]];
[orderLbl setTextAlignment:NSTextAlignmentCenter];
[cell.contentView addSubview:orderLbl];
}
[(UILabel *)[cell.contentView viewWithTag:1] setText:[NSString stringWithFormat:#"test"]];
return cell;
}
Better you can use Custom Textlabel with your desired frame requirement.
Give a tag for the textlabel and find out the label in cellForRowAtIndexPath as following :
UILabel *myLabel = (UILabel *)[cell.contentView viewWithTag:givenTagValue];
Then apply your required behaviour for this label as you want.
Hope it helps..
In such cases I prefer creating custom subclass and apply layout as I wanted so if I get any autolayout warning I always know it's because layout is not correct.
Actually, changing the frame the textLabel in cell is NOT allowed in TableView. The width of frame for the textlabel of each cell relates to the cell, developer couldn't set it programmatically. However, I worked out a feasible solution: we may not change the frame of the textLabel, but we may change the source text alternatively. If we can truncate the source string in advance, this problem is overcome.
//Assign each element in the array a row in table view
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var myCell = myTableView.dequeueReusableCell(withIdentifier: "The Cell")
if myCell == nil {
myCell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "The Cell")
}
myCell?.accessoryType = UITableViewCellAccessoryType.none
// If the text is too long (longer than 25 chars), the text will be truncated
// The lenght of label cannot be set effectively here, so I truncate the source string alternatively
var myString: String = sectionArray[indexPath.row]
if myString.characters.count > 25 {
let myIndex = myString.index(myString.startIndex, offsetBy: 25)
myString = myString.substring(to: myIndex)
myString += "..."
}
myCell?.textLabel?.text = myString
myCell?.detailTextLabel?.text = "\(NSDate())"
return myCell!
}
That supports to work.
I have a UITableView with different cell heights. I have added a custom separator line also using the following code
UIView* separatorLineView = [[UIView alloc]initWithFrame:CGRectMake(1, cellReport.frame.size.height-1, table.frame.size.width-1, 1)];
separatorLineView.backgroundColor = [UIColor grayColor];
[cellReport.contentView addSubview:separatorLineView];
When i scroll my table extra separators appear between the rows and i dont understand why? Am i missing something? help me out.
In short UITableView displays separator at wrong position for some cells. Have attached an example image for reference.
P.S: I AM NOT USING AUTOLAYOUT
Please follow these steps
And do not need to every time your separator view. And set your frame every time.
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *myIdentifier=#"Cell";
UITableViewCell *Cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:myIdentifier];
UIView* separatorLineView;
if (Cell == nil)
{
Cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:myIdentifier];
separatorLineView = [[UIView alloc] initWithFrame:CGRectZero];
separatorLineView.backgroundColor = [UIColor grayColor];
separatorLineView.tag = 100;
[Cell.contentView addSubview:separatorLineView];
}
separatorLineView = (UIView *)[Cell.contentView viewWithTag:100];
separatorLineView.frame = CGRectMake(0, Cell.frame.size.height-1, Cell.frame.size.width, 1);
}
I'm setting UITableView separator color this way:
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableView.separatorColor = [UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.1f];
}
And SOMETIMES on iOS7 my table view separators look different (cells with data and empty cells separators have different alpha I guess), look at attached image:
How can I solve this issue?
If you want to remove all cell's separator which has empty content then use following line of code.
self.tableView.tableFooterView = [[UIView alloc] init];
in viewDidLoad method.
Other wise set self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; and set your custom UIImagefor separator.
EX:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *CellIdentifier = #"cell"; //[NSString stringWithFormat:#"S%1dR%1d", indexPath.section, indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
.
. // your other code.
.
UIImageView *imgLine = [[UIImageView alloc] init];
imgLine.tag = 101;
[cell.contentView addSubview: imgLine];
}
.
. // your other code.
.
UIImageView *imgLine = (UIImageView *) [cell.contentView viewWithTag:101];
imgLine.frame = CGRectMake(5, 69, 310, 1);
imgLine.image = [UIImage imageNamed:#"queTblLine.png"];
return cell;
}
And don't forget to set self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
EDITE :
Not sure but might be helpful in your case:
set self.tableView.separatorInset = UIEdgeInsetsZero;
Get From question/answer.
Swap the order of the two statements. Set the color first, then the inset:
self.tableView.separatorColor = [UIColor redColor];
self.tableView.separatorInset = UIEdgeInsetsZero;
You can achieve this visually in storyboard or nib/xib
Put UIImageView at bottom edge of cell and you can set image as per your requirement
STEP 1: Set divider as per image
STEP 2: Set Separator style to none
You can set it programatically:
- (void)viewDidLoad {
[super viewDidLoad];
[[UITableView appearance] setSeparatorColor:[UIColor redColor]];
}
In my iPhone application UITable having So many cells with Text. The Text size is more than the width of the device. How can I wrap the text in cell and display the text in the different lines or divide it in the lines?
You can add a UILabel as a subview of a cell in a
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
method. Then set its lineBreakMode and numberOfLines properties as you want. The code must look something like this:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tID];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:tID] autorelease];
}
else{
UIView *tView = [cell viewWithTag:100];
[tView removeFromSuperview];
}
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 90, 50)];
label.tag = 100;
label.numberOfLines = 2;
label.lineBreakMode = UILineBreakModeWordWrap;
label.text = #"Putyourverylongcelltexthere";
[cell addSubview:label];
[label release];