how to wrap the text in UITable cell in Iphone? - ios

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];

Related

How to change the width of tableView cell textLabel

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.

Separator line issues - uitableview cells with different heights

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);
}

Same Cells repeat after Scrolling

I am using table view using custom coding with tag method to save memory.
I was successful to show data in the view but the problem is if 10 cells are showing and then if I scroll down like for one cell then it should show 2-11 cell data but it switches to 1-10 again.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UILabel *cellNAMElabl = nil;
UILabel *cellDetaillabl = nil;
UIImageView *imgView = nil;
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
cellNAMElabl = [[UILabel alloc] initWithFrame:CGRectMake(88, 10, 150, 20)];
[cellNAMElabl setTag:1];
cellNAMElabl.text = [name5 objectAtIndex:indexPath.row];
UIFont *myFont1 = [ UIFont fontWithName: #"Arial" size: 20.0 ];
cellNAMElabl.font = myFont1;
[cell.contentView addSubview:cellNAMElabl];
cellDetaillabl = [[UILabel alloc] initWithFrame:CGRectMake(88, 28, 150, 20)];
[cellDetaillabl setTag:2];
cellDetaillabl.text = [email5 objectAtIndex:indexPath.row];
UIFont *myFont = [ UIFont fontWithName: #"Arial" size: 13.0 ];
cellDetaillabl.font = myFont;
[cell.contentView addSubview:cellDetaillabl];
imgView=[[UIImageView alloc] initWithFrame:CGRectMake(25, 5, 52, 50)];
[imgView setTag:3];
imgView.image = [imagepath5 objectAtIndex:indexPath.row];
[cell.contentView addSubview:imgView];
}
cellNAMElabl = (UILabel *)[cell viewWithTag:1];
cellDetaillabl = (UILabel*)[cell viewWithTag:2];
imgView = (UIImageView*)[cell viewWithTag:3];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
You are not assigning new content to subviews, if they have been already created. After the case if(cell == nil), these you have just got references.
cellNAMElabl = (UILabel *)[cell viewWithTag:1];
cellDetaillabl = (UILabel*)[cell viewWithTag:2];
imgView = (UIImageView*)[cell viewWithTag:3];
Here when cell is not nil, you are just getting references to labels and imageview, but you are not setting new text and image from data source. Add following lines and remove them from the if (cell == nil) part:
cellNAMElabl.text = [name5 objectAtIndex:indexPath.row];
cellDetaillabl.text = [email5 objectAtIndex:indexPath.row];
imgView.image = [imagepath5 objectAtIndex:indexPath.row];
[cell.contentView addSubview:imgView];
The way this dequeueReusableCellWithIdentifier works: If iOS detects that a cell is not displayed anymore, then dequeueReusableCellWithIdentifier will return that cell. If there is no unused cell, it returns nil. So what you need to do:
If dequeueReusableCellWithIdentifier returns nil, then you create a new cell, and you do all the setup that is required for all cells with the same identifier. For example, add view tags like you did, set fonts, colors etc.
Then, whether you use a cell returned by dequeueReusableCellWithIdentifier or one that you just created yourself, you add all the information that is used for the specific section/row that you want to display. So if row 1, row2, and so on display different text, then you set the text here. That's what you didn't do, so when a cell was reused, you didn't set the new text for it.
So the idea is that all the work that is the same for all rows is only done once when a cell is created, and only as many cells are created as is needed to display them on the screen. The work that is different from row to row is done for each row, as it is needed.
If you set a breakpoint in the if cell == nil block its probably only being hit for the first set if your reuseID is correct. Thats why its never getting a chance to set any new data into the cell.
You should not look for a nil cell, rather use a correct reuseID and a prototype cell in IB that is set to a custom UITableViewCell subclass you create.
Its also good practice to implement prepareForReuse on custom cells, where you clear any cell data e.g. label.text = nil, imageview.image = nil
This way you dont get invalid data from previously dequeued cells. It might not solve the question directly, but it would have wiped the fixed data set in your if cell == nil block to help debug.
What you want to do is..
add/setup the tableViewCell UI if the cell is nil..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"cellID";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];
UILabel *cellNAMElabl = [[UILabel alloc] initWithFrame:CGRectMake(88, 10, 150, 20)];
cellNAMElabl.tag = 1;
cellNAMElabl.font = [UIFont fontWithName: #"Arial" size: 20.0 ];
[cell.contentView addSubview:cellNAMElabl];
UILabel *cellDetaillabl = [[UILabel alloc] initWithFrame:CGRectMake(88, 28, 150, 20)];
cellDetaillabl.tag = 2;
cellDetaillabl.font = [UIFont fontWithName: #"Arial" size: 13.0 ];
[cell.contentView addSubview:cellDetaillabl];
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(25, 5, 52, 50)];
imgView.tag = 3;
[cell.contentView addSubview:imgView];
}
//and just update your data if the cell is currently exist and not nil..
//you already called the view using tag so, you dont need those:
// UILabel *cellNAMElabl = nil;
// UILabel *cellDetaillabl = nil;
// UIImageView *imgView = nil;
((UILabel *)[cell viewWithTag:1]).text = [name5 objectAtIndex:indexPath.row]; // cellNAMElabl
((UILabel*)[cell viewWithTag:2]).text = [email5 objectAtIndex:indexPath.row]; // cellDetaillabl
((UIImageView*)[cell viewWithTag:3]).image = [imagepath5 objectAtIndex:indexPath.row]; // imgView
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
hope this have help you, happy coding cheers!

How to Implement DataSheet

I want to implement data sheet to display history of user. I want to implement that design like this:
But I dont know how to do that...Can anyone please help me
Edit:
Add the horizontal line at the specific position and the label at the position and it will look like this.
Create a tableview and than in cellForRowAtIndexPath method add this code..
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *SimpleTableIdentifier;
UITableViewCell * cell;
SimpleTableIdentifier = #"SimpleTableIdentifier";
cell = [tableView dequeueReusableCellWithIdentifier: nil];
if(cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier];
UILabel * numLbl = [[UILabel alloc] initWithFrame:CGRectMake(0,5,33,30)];
numLbl.text = #"1";
[numLbl setFont:[UIFont fontWithName:#"Helvetica" size:10.0]];
numLbl.backgroundColor = [UIColor clearColor];
[cell addSubview:numLbl];
UILabel * nameLbl = [[UILabel alloc] initWithFrame:CGRectMake(30,5,50,30)];
nameLbl.text = #"john%Lakeview";
[nameLbl setFont:[UIFont fontWithName:#"Helvetica" size:10.0]];
nameLbl.backgroundColor = [UIColor clearColor];
[cell addSubview:nameLbl];
//create a hoizontal separator in cell to display it like column
UIView* hSeparatorview1 = [[UIView alloc] initWithFrame:CGRectMake(25, 0, 1, 30)];
hSeparatorview1.backgroundColor = [UIColor blackColor];
hSeparatorview1.tag = 1;
[cell addSubview:hSeparatorview1];
UIView* hSeparatorview2 = [[UIView alloc] initWithFrame:CGRectMake(85, 0, 1, 30)];
hSeparatorview2.backgroundColor = [UIColor blackColor];
hSeparatorview2.tag = 2;
[cell addSubview:hSeparatorview2];
}
return cell;
}
//this method is used to set the hight of the tableview cell
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
{
return 30;
}
I have created it for only two label and two horizontal view but you can create as many as you like.
And yes dot forget to place this code in didSelectRowAtIndexPath otherwise horizontal view will disappear when user click the cell.
- (void)tableView:(UITableView *)atableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//get the cell which is selected
UITableViewCell *selectedCell = [atableView cellForRowAtIndexPath:indexPath];
//set cell horizontal saparator view color of selected cell bcoz when cell selected all view color is gone
UIView *hSeparatorview1=[selectedCell viewWithTag:1];
hSeparatorview1.backgroundColor = [UIColor blackColor];
UIView *hSeparatorview2=[selectedCell viewWithTag:2];
hSeparatorview2.backgroundColor = [UIColor blackColor];
}
Better to try with Custom tableview Cell for text,and UIViews for lines.I'm also done the same in my app.

Stop image expanding in multiline table row?

I'm having problems implementing a table row that allows text to wrap to multiple lines, and also has an image on the left, and a disclosure accesssory on the right.
The multiline text is fine but the imageView expands to the right when there is more than one line of text. I want images in all rows to be the same size. I've tried setting the autoresizingMask to UIViewAutoresizingNone but this doesn't seem to work.. Do I need to use the contentView, or a nib file?
Any help/example code appreciated!
If all off your images are different size then you should think of adding a ImageView as Subview to cell. Keep Labels depending on the Imageview.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell;
UIImageView *cellImage;
UILabel *label;
UILabel *detailLabel;
cell = [tableView dequeueReusableCellWithIdentifier:#"cell"]; //Create cell
if(cell == nil)
{
cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"] autorelease];
cellImage = [[[UIImageView alloc] initWithFrame:CGRectMake(3, 3, 44, 44)] autorelease];
label = [[[UILabel alloc] initWithFrame:CGRectMake(55, 4, 260, 20)] autorelease];
label.font = [UIFont boldSystemFontOfSize:15.0];
label.tag=25;
detailLabel = [[[UILabel alloc] initWithFrame:CGRectMake(55, 25, 260, 15)] autorelease];
detailLabel.font = [UIFont systemFontOfSize:13.0];
detailLabel.tag=30;
[cell.contentView addSubview:cellImage];
[cell.contentView addSubview:label];
[cell.contentView addSubview:detailLabel];
cell.accessoryType =UITableViewCellAccessoryDisclosureIndicator;
}
else
{
cellImage = (UIImageView *)[cell.contentView viewWithTag:20];
label = (UILabel *)[cell.contentView viewWithTag:25];
detailLabel = (UILabel *)[cell.contentView viewWithTag:30];
}
cell.image = [arrayContainingImages objectAtIndex:indexPath.row];
label.text =[arrayContaininglabeltext objectAtIndex:indexPath.row];
detailLabel.text=[arrayContainingDetailLabelText objectAtIndex:indexPath.row];
return cell;
}
pls see Stop image expanding in multiline table row? #2

Resources