Configuring UITableViewCell text dynamically - iphone

Here is my problem: I have UITableView and I want to dynamically configure "THE Cell" text with different text fonts e.g. first line will be 12, Helvetica, Bold and the second line will be 10, Helvetica. Also note that number of lines is unknown and needs to be dynamically determined. Any help appreciated!
ps: Apparently question isn't understood well. I didn't mean to show each line in different cells. So think about only one cell and configuring the text for this particular cell. I have a dynamically determined number of lines for this cell so it could be 2 or 3 depending on the availability of the information. And I want this lines to have different fonts and different colors. One way to go for this is to have dynamic number of UILabel for the cell, but I would like to see if there is other options?

I can't really answer this very well without an example pattern but here it goes:
In - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath add your customisations. Here's some examples:
1)
if (indexPath = 1) { //row 1
cell.detailTextLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:12]; //Important note: -Bold can vary per font. For example, Arial's bold variant is 'Arial-BoldMT'.
//change to needs
}
else if (indexPath = 2) { //row 2
//etc.
2)
if (indexPath <= 6) { //row 1-6
cell.detailTextLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:12]; //Important note: -Bold can vary per font. For example, Arial's bold variant is 'Arial-BoldMT'.
//change to needs
}
else if (indexPath >=7 && indexPath <=15) { //rows 7-15
//etc.
3)
///etc.
else if (indexPath >=84) { //rows 84 and over
cell.detailTextLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:12];
}
Unless you have a specific pattern, it can be hard if you don't know how many rows there are. If you need any more help just comment below.

This is fairly trivial to do. Just set the content of the cells in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
You can adjust the font size, color, etc based on the indexPath of the cell.
If you want a plethora of examples of what all is possible, then check out Apple's Table View Programming Guide.

Related

UITableView sections issue with different cell colors

I'm a newbie to iPhone development. I have a table view with multiple sections in it. I'm changing the color of cells like this and it works fine.
if ([indexpath row]%2)
{
trackCell.contentView.backgroundColor = [UIColor clearColor];
}
else
{
trackCell.contentView.backgroundColor = [UIColor colorWithRed:212/255.0 green:212/255.0 blue:212/255.0 alpha:0.1];
}
But now the last cell from one section has the same color as the first cell in the next section. How can I solve this?
This is pretty dirty, but you could count up all the cells before your current one and then use that count to work out the required color. This is untested, but something like the following should do it:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
int cellCount = 0;
for (int i=0;i<indexPath.section-1;i++)
{
cellCount+=[self numberOfRowsInSection:i]
}
cellCount+=indexPath.row;
if(cellCount%2==0)
{
//set one color
}
else
{
//set other color
}
// blah
}
the problem is that if cell is in section 1 (or 2, 3, ...)
and the previous section has an odd number of cells, it ends with the same color of the last cell of previous cell.
so, you need to check the previous section number of cells, and if that number is odd, invert your "if" statement, and leave it as it is now if it is even
EDIT:
int toBeAdded = 0;
if (indexPath.section>0) {
toBeAdded = ([self tableView:tableView numberOfRowsInSection:(indexPath.section -1)]) %2 ;
}
if (([indexpath row]+toBeAdded)%2) {
trackCell.contentView.backgroundColor = [UIColor clearColor];
} else {
trackCell.contentView.backgroundColor = [UIColor colorWithRed:212/255.0 green:212/255.0 blue:212/255.0 alpha:0.1];
}
EDIT 2:
seeing "Will Jenkins"... he's right... you need to see all previous sections, not just the previous one...
his way to count all cells in a section is not so good and fast as mine, but his answer is the right one, +1 for him... anyway, the best way could be to combine his loop and my call to tableView:tableView numberOfRowsInSection:
Regaridng section change color, need to write your own logic like,
Need to use Static int value irrespective of sections and use already using code with replacing
indexpath.row with your value. i.e.
Static int Index;
if ( Index % 2)
{
trackCell.contentView.backgroundColor = [UIColor clearColor];
} else {
trackCell.contentView.backgroundColor = [UIColor colorWithRed:212/255.0 green:212/255.0 blue:212/255.0 alpha:0.1];
}
Index++;
It happens due to your Number of Rows in each section...
Suppose you are trying to provide RED color to Odd Rows and GREEN color to Even Rows and you have 3 rows in First Section then last cell will have RED color and First cell of Second Section also has RED color.
So you just need to check the number of rows in Previous Section. If it is Even Number then you should Maintain the Order of Color , otherwise simplly Change the Order of Color for the Section.

UITableView showing more rows than specified in numberOfRowsInSection:

I want my tableView to show 6 rows with text in it, in this case "Example." As far as I can tell, I have my numberOfSectionsInTableView: and numberOfRowsInSection: set properly. See example code below:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// Return the number of rows in the section.
return 6;
}
- (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];
}
cell.textLabel.text = #"Example";
return cell;
}
The problem is when you see the image below showing lines for rows that shouldn't/don't exist.
How do I get rid of the lines showing past row 6?
The generally accepted way of doing this is to add a footer view with a frame size of CGRectZero, as such:
[tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]]
What this does is tell the table that there is a footer, and so it stops displaying separator lines. However, since the footer has a CGRectZero as its frame, nothing gets displayed, and so the visual effect is that the separators simply stop.
Swift Version
The easiest method is to set the tableFooterView property:
override func viewDidLoad() {
super.viewDidLoad()
// This will remove extra separators from tableview
self.tableView.tableFooterView = UIView(frame: CGRect.zero)
}
This is Because of Your Table-view Height. Weather you have Write
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// Return the number of rows in the section.
return 6;
}
But its show rows According to Table-view Size. If you Dont want to show This extra Lines then Make UITableView Style Plain To Grouped.
Short and simple answer..
self.tableView.tableFooterView = [UIView new];
You could do something along the lines of:
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:7 inSection:0];
[self.mytableView cellForRowAtIndexPath:indexPath].hidden = YES;
Im sure there are some better ways but this is the first thing that came to mind.
If you're referring to the light gray lines that appear below the last row, that's simply the default way a UITableView draws the row separator.
You could try changing the Separator style in Interface Builder (see the images below) to see if one of those might be more to your liking.
You didn't say what you do want to see past the last row. If you just want to see the window background, then just embed your table view in a UIView that's just tall enough to show the number of rows you want to see. If you want to see more rows without scrolling, then you would have to adjust the size of that containing view based on the number of rows.
To programmatically remove it, use this:
[yourTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
It's a lot easier to:
return numberOfSections + 1
return 0 rows in the final section
This keeps it simple!

Table view cells overlapping when deleting

I have text in a table view cell which displays in one line. When the 'edit' button is pressed and the negative sign is pressed to display the delete button. This pushes the text in the cell into two lines causing the cells to overlap. My cells also have subtitles. I have text in other cells that take two lines and when deleting get truncated. I set cell.textLabel.numberOfLines = 2 to do this so that's not a issue.
How can I get the one line text to only display in one line when the delete button appears? keeping the two line text in the cells as is.
Any help will be appreciated! Thanks
I managed to find the solution...it isn't as complicated as I thought
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath {
Favorites *favorite = [fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text = favorite.name;
NSLog(#"%d",[favorite.name length]);
NSLog(#"%#",favorite.name);
if ([favorite.name length] <= 34) {
cell.textLabel.numberOfLines = 1;
NSLog(#"no. of lines = 1");
}else{
cell.textLabel.numberOfLines = 2;
NSLog(#"no. of lines = 2");
}
cell.detailTextLabel.text = favorite.subTopic;}
The configureCell method is called in my - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
Basically I'm checking for the number of characters in the string. I found the magic number to be 34 characters so that everything fits.

UITableView sections text alignment

can someone help me with text alignment to the right in my uitableview sections ?
the defult alignment is left.
if the only way is to build custom header i will appreciate for code example.
try with this
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
//create a uitextfield and set font you want in
return uitextfield ;
}
it seems working for me.
Have you tried adding
cell.textLabel.textAlignment = UITextAlignRight;
to tableView:cellForRowAtIndexPath:?
As long as you are using cell.textLabel.text, and not the deprecated cell.text to set the text of the cell, this should work.
edit:
Sorry, I didn't catch that you were trying to do this with the section headers. I think you're going to have to roll your own header views, and return them using the UITableView delegate method -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
Could take some work to get them to look like the default ones. Here is an example of someone describing how to theme the section headers: http://iphoneinaction.manning.com/iphone_in_action/2009/07/beauti.html
try this it is suitable for right to left languages
-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
UILabel *lab = [[self.tableData objectAtIndex: section] valueForKey:kSectionTitleKey];
lab.Text = #"right to left alignment text";
lab.textAlignment = UITextAlignmentRight;
return lab.text;
}
as you see in code this is the only way to align text from right to left, create a custom label, set its text property and alight the label to the right.

Can't Reorder a UITableViewCell into _some_ empty sections of a UITableView

If I have a UITableView in edit mode, w/ reordering turned on, it seems I can't move some (but not all) cells into some (but not all) empty sections. For example, if I have this layout :
Section 1
apple
banana
Section 2
doberman
Section 3
Section 4
Then I can move 'doberman' into any slot in section 1 (except after 'banana'), but I can't move it into section 3 or 4 at all.
On the other hand, I can move 'apple' & 'banana' into section section 2 (except after 'doberman'), and I CAN move it into section 3, but NOT into section 4.
What gives? this seems like buggy behavior. How do people work around it? Is apple aware of this bug?
Yup, it's an apple bug. I talked to apple, used one of my "support tickets" for them to tell me that it's their bad.
I did also hack around it, by moving from the obvious many section table model to a model that only has a single section, and models "section headers" as styled table view cells.
Stupid apple...
I have used another hack: create a dummy last section with an empty cell (invisible backgroundView) in the edit mode. With this, i can keep the section/row organization, and the reordering works wonderfully, even with the "last" section.
Another way, is to have a section footer with, at least, the same height as your cells. In this case, the cell can go past the last cell of the last section.
In this blog tells you the easiest way to do this:
http://davemeehan.com/technology/moving-uitableviewcell-between-sections-in-grouped-uitableview
No matter if the section is empty. Simply return some text in titleForFooterInSection: and you'll be able solve the problem. It even works by returning #"".
Sirdek is correct. Just copy the following code into your UITableViewDelegate/UITableViewDataSource.
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
NSInteger lastSection = [self numberOfSectionsInTableView:tableView] - 1;
NSInteger lastRow = [self tableView:tableView numberOfRowsInSection:lastSection] - 1;
CGFloat height = [self tableView:tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:lastRow inSection:lastSection]];
return (section == lastSection ? [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, height)] autorelease] : nil);
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
NSInteger lastSection = [self numberOfSectionsInTableView:tableView] - 1;
NSInteger lastRow = [self tableView:tableView numberOfRowsInSection:lastSection] - 1;
CGFloat height = [self tableView:tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:lastRow inSection:lastSection]];
return (section == lastSection ? height : 0);
}