How to put text on the right side of the table header - objective-c

In UITableView we have headers
For example
Say I want to group businesses by the building they are in.
BuildingA 10
Business1
Business2
Business3
Business4
How do I arrange so that the 10 is on the right side of the header

You can override tableView:viewForHeaderInSection: to add your own extra label whose rect is somewhere on the right side, like CGRectMake(280,5,30,20);. You should also set it so the text aligns right.
Note that if you do this, you also have to provide the left-side label (since it is no longer there) and you must override tableView:heightForHeaderInSection: as well.

In that u can use the custom header cell then that cell attach in the header.
for that following code is implement.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 2;
}
-(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UIView *v=[[[NSBundle mainBundle] loadNibNamed:#"tHeader" owner:self options:nil] objectAtIndex:0];
[(UILabel*)[v viewWithTag:1] setText:[NSString stringWithFormat:#"%i",section+1]];
return v;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return (section==0)?5:((section==1)?7:((section==2)?3:4));
}
// Customize the appearance of table view cells.
- (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] autorelease];
}
cell.textLabel.text=[NSString stringWithFormat:#" Hello-%i",indexPath.row];
// Configure the cell.
return cell;
}
download source from here.

Related

adding the row values in the uitableview and display the added value in final row of the uitableview using objective c

-(UITableViewCell *)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath{static NSString *cellIdentifier = #"Cell";
customcell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[customcell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.finacialyear.text=[self->entries1 objectAtIndex:indexPath.row];
cell.term.text=[self->entries2 objectAtIndex:indexPath.row];
cell.balance.text=[self->entries3 objectAtIndex:indexPath.row];
cell.tintColor = [UIColor blueColor];
return cell;
}
have to add the rows values in the tableview cell and display in to the final row.can anyone suggest me how to do this in objective c.
thanks
First you need to add one extra row:
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return <#rows you have now#> + 1;
}
Then you should return a different cell if you are in the last row:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (isLastRow) {
return BalanceSumCell;
} else {
return customcell;
}
}
Remember to name your classes with a capital first letter (CustomCell and not customcell).
If you want to do different calculations when the user selects a cell, you should do that in -didSelectRowAtIndexPath:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Keep track of user's selection and update the last cell as needed
}
In general, your question is broad and if you break it down to smaller problems you will find a lot of related answers to help you.
Good luck!

How to edit UITableViewCell frame position programmatically

I need to edit the position of an UITableViewCell programmatically, this is the relevant code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"leftMenuCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
}
if([[[self.arForTable objectAtIndex:indexPath.row] valueForKey:#"level"] intValue] == 1)
{
CGRect frame = cell.textLabel.frame;
frame.origin.x= 30;
cell.textLabel.frame= frame;
}
return cell;
}
Using breakpoints I can see that the if condition works but the code to edit the frame position does nothing. Is it possible to edit the position like this or i'll have to subclass UITableViewCell?
I think you should create 2 subclasses of UITableViewCell with 2 different identifiers depending on the one you want to display if they are different.
You could customize frame in each subclass as you want and it will be easier for you to work with it

Search Bar in UITableView doesn't display text in Cell label

Here's where the magic isn't happening:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"songCell";
songViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
long row = indexPath.row;
if (cell == nil) {
cell = [[songViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if (tableView == self.searchDisplayController.searchResultsTableView) {
cell.songLabel.text = _searchResults[row];
} else {
cell.songLabel.text = _songListArray[row];
}
return cell;
}
I know the _searchResults array is populated with the correct search results and I've edited the numberOfRowsPerSection appropriately. The filter is working correctly, but it won't display the _searchResults[row] text while typing into the search bar. If I don't use the bar, the cells are populated correctly with _songListArray[row].
Something is going wrong in:
if (cell == nil) {
cell = [[songViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
If I don't include the if expression, I get an error. How should I initialize the prototype cell while the search bar is in use? All I get is empty labels, but I know the search is working because if there are no filtered results in the array the table says "No Results" in the middle. Why isn't my songLabel.text updating?? It won't even display text when I set the label to #"HELLO??" instead of the array[row].
You need to have the searchResultsTableView dequeue a cell, not just your main table view. The cellForRowAtIndexPath method should look something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == self.tableView) {
RDTriangleCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
cell.textLabel.text = self.theData[indexPath.row];
return cell;
}else{
UITableViewCell *cell = [self.searchDisplayController.searchResultsTableView dequeueReusableCellWithIdentifier:#"SearchCell" forIndexPath:indexPath];
//RDCell *cell = [self.searchDisplayController.searchResultsTableView dequeueReusableCellWithIdentifier:#"SearchCell" forIndexPath:indexPath];
cell.textLabel.text = self.filteredData[indexPath.row];
return cell;
}
}
In viewDidLoad, you should register the class if you use the code I show above for a standard UITableViewCell.
[self.searchDisplayController.searchResultsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"SearchCell"];
If you use a custom cell for the search results table, then you should use something like the line I have commented out, and register the nib for that custom cell.

How can I display a thumbnail image to Table Cell in xCode?

As a Xcode beginner, I want to display a thumbnail in the table cell in my app. For now, I have this code which parses JSON data and posts it in the title and subtitle of the cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MainCell"];
if(cell == nil){
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MainCell"];
}
cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:#"receta"];
cell.detailTextLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:#"koha"];
return cell;
}
How can I show a thumbnail in the right of the cell?
Thanks.
If you don't want to make a custom cell, you can create a UIImageView with your desired image and set it as the cell's accessoryView, which will show it on the right edge of the cell. You also need to ensure that the height of the cell is tall enough to fit the image's height.
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 100;
}
- (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.accessoryView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"some-image-name"]];
return cell;
}
You can return a constant (I chose 100) if the image will always be a fixed size, or you can check the UIImage's size and return something like size.height + 10 for some extra padding.

Send NSArray to NSTableView

What is the easiest way to send the data stored in an NSArray to a NSTableView and display it line by line?
For example:
NSArray has data [a, b, c]
I want the NSTableView to say:
a
b
c
The NSTableView only needs 1 column.
You don't "send" things to NSTableView. NSTableView asks you for things. It does so via the NSTableViewDataSource protocol. So all you need to do is implement the two required methods from that (-numberOfRowsInTableView: and -tableView:objectValueForTableColumn:row:), and connect the tableview's data source outlet to your object.
Documentation for NSTableViewDataSource is here: https://developer.apple.com/DOCUMENTATION/Cocoa/Reference/ApplicationKit/Protocols/NSTableDataSource_Protocol/Reference/Reference.html
You need to explore UITableViewDelegate and UiTableViewDataSource delegate methods:
#pragma mark --- Table View Delegate Methods ----------------------------
//Handles the selection of a cell in a table view
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
//Defines the number of sections in a table view
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
//Defines the header of the section in the table view
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return nil;
}
//Defines the number of rows in each section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
//Defines the content of the table view cells
- (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] autorelease];
}
cell.textLabel.text = [myDataArray objectAtIndex:[indexPath row]];//<-pay attention to this line
return cell;
}