After lots of searching, I can't seem to find what I'm looking for.
I have a UITableview where some of the sections may be blank to begin with. Here's a picture to help get an idea of what I'm talking about. I want to have some TEXT (not a table cell) in the middle between the footer and the header. Is there anything that I may have overlooked?
What I did was create a UILabel with the same size as the tableview and add it to the tableview, something like:
UILabel* emptyLabel = [[UILabel alloc] init];
emptyLabel.textAlignment = UITextAlignmentCenter;
emptyLabel.backgroundColor = [UIColor clearColor];
emptyLabel.frame = self.tableView.bounds;
emptyLabel.text = #"Empty";
[self.tableView addSubview:emptyLabel];
You can then use the hidden property to show it or hide it, e.g. emptyLabel.hidden = TRUE;
Because of the nature of UITableViews, I'm not sure you could achieve replacing the UITableCell view with something else. However there's no reason you can't completely alter the table cell itself to look like a plain UITextLabel and not a cell! You could do something like this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
/* Initial setup here... */
if (thisCellHasNoDataYet) {
// Prevent highlight on tap
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.text = #"TEXT YOU WANT THE 'CELL' TO DISPLAY";
// etc...
}
else {
// Otherwise we have data to display, set normal cell mode
[cell setSelectionStyle:UITableViewCellSelectionStyleBlue];
cell.backgroundColor = [UIColor whiteColor];
// etc...
}
The benefit here is that once your condition is met, you just have to set the boolean (I used thisCellHasNoDataYet) to TRUE and call reloadData on your table!
Related
I just started Xcode objective-c recently and right now I'm trying to create a tableview with textfields in them to type. Ive looked into other stack overflow questions but many are from 6-8 years ago and seem to have a wide rage of answers and extremely complex. Could someone help me with the basics of how I can insert a textfield in a table view and give me some advice. Thanks!
Write this code in uitableview cell
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(110, 10, 185, 30)];
textField.clearsOnBeginEditing = NO;
textField.textAlignment = UITextAlignmentRight;
textField.delegate = self;
[aCell.contentView addSubview:txt];
You can do this as follows:
Drag and drop a UITableView in to your view
Drag and drop a UITableViewCell into your table.
Drag and drop a UITextField (or any
other UI component that you need)
I would suggest you to please refer tutorials for that like
1.https://videos.raywenderlich.com/courses/22-table-views-in-ios/lessons/8
2.https://www.appcoda.com/expandable-table-view/
They have best tutorials with all steps you can easily do whatever you want.
I hope it will help you.
Thanks.
Try below code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MyIdentifier"];
if (cell == nil) {
/*
* Actually create a new cell (with an identifier so that it can be dequeued).
*/
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"MyIdentifier"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
/*
* Now that we have a cell we can configure it to display the data corresponding to
* this row/section
*/
UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(45, 30, 200, 40)];
tf.textColor = [UIColor colorWithRed:0/256.0 green:84/256.0 blue:129/256.0 alpha:1.0];
tf.font = [UIFont fontWithName:#"Helvetica-Bold" size:25];
tf.backgroundColor=[UIColor whiteColor];
tf.text=#"Hello World";
[cell.contentView addSubview:tf];
/* Now that the cell is configured we return it to the table view so that it can display it */
return cell;
}
You can create a custom cell. Add the textfield in it and use that in the table by registering the nib or class.
I have a tableview with values that could be the same.
When selecting the cell, the textfield value will be populated with the selected cell.
I want to only highlight the row that was selected rather than highlight all the values that are the same as the textfield.
My approach currently:
where data is an array of possible values
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"identifierCell"];
}
NSString *value = ([self.data objectAtIndex:indexPath.row]);
NSString *selectedValue = textField.text;
if ([value floatValue] == [selectedValue floatValue]) {
cell.backgroundColor = [UIColor colorWithRed:220.0/255.0 green:220.0/255.0 blue:220.0/255.0 alpha:1.0];
cell.textLabel.textColor = [UIColor whiteColor];
}
else {
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor blackColor];
}
Side note: I have a couple of these drop down fields that can change the textfield text depending on which row was selected. (i.e. if row 1 was selected in the other dropdown, then 1.391 would be populated in the textfield text.
Not sure how relevant that will be.
How would I check only the row that was selected and populate the textfield per the image below?
Update: Thanks to #k06a using indexPath is a step forward, however the problem now arises where when if row A is selected, the indexPath for B should not change. I was thinking of setting 2 different indexPath variables and on didSelectRowAt only set those for the methods I'm calling to change the other respective values
You can enumerate all cells within a loop to select and deselect rows based on selectedValue and value equality. This loop will deselect previous row, and select a new one. And not forget to setup initial state after dequeing reusable cell. All cells can be fetched with: self.tableView.visibleCells
Or you can just deselect previous cell by remembering its indexPath. And select a new one and remember its indexPath. This is the most effective variant.
Or you can just call [self.tableView reloadData] when wanna change selection. This will be less effective, but shorter and easier to implement.
UPDATE:
Just use different condition of cell selection not basing on value but basing on indexPath. And remind me why do you need manual selection of cell? I mean manual setting backgroundColor and textColor can be automatic by [UITableViewCell setSelected:] selection.
My solution thanks to #k06a
NSIndexPath *selectedIndexPathA;
NSIndexPath *selectedIndexPathB;
In didSelectRowAtIndexPath I pass in the indexPath to the method that will set these values:
[self setOtherFieldsWithIndexPath:indexPath];
and in setOtherFieldsWithIndexPath method
if (rowA) { selectedIndexPathA = indexPath; }
else if (rowB) { selectedIndexPathB = indexPath; }
else {...}
then finally in cellForRowAtIndexPath
if (selectedIndexPathA.row == indexPath.row) {
cell.backgroundColor = [UIColor colorWithRed:220.0/255.0 green:220.0/255.0 blue:220.0/255.0 alpha:1.0];
cell.textLabel.textColor = [UIColor whiteColor];
}
else if (selectedIndexPathB.row == indexPath.row) {
cell.backgroundColor = [UIColor colorWithRed:220.0/255.0 green:220.0/255.0 blue:220.0/255.0 alpha:1.0];
cell.textLabel.textColor = [UIColor whiteColor];
}
else {
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.textColor = [UIColor blackColor];
}
Not sure if this is the best solution but, it currently works the way I want it to work.
I am having two labels created manually for displaying it in the tableviewcell named title and detail, code for displaying it are,
dealarray = [[NSMutableArray alloc]initWithObjects:#"1",#"2",#"3",#"4",nil];
detailarray = [[NSMutableArray alloc]initWithObjects:#"oneoneoneoneone oneoneoneoneoneoneoooooooo",#"two",#"three",#"fouronefouronefouronefouronefouronefouronefouron",nil];
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
for(UILabel *lbl in [cell.contentView subviews])
{
[lbl removeFromSuperview];
}
cell.accessoryType= UITableViewCellAccessoryNone;
UILabel* title;
title= [[UILabel alloc] initWithFrame:CGRectMake(5,5,300,20)];
[cell.contentView addSubview:title];
[cell.contentView bringSubviewToFront:title];
[title setFont:[UIFont boldSystemFontOfSize:14]];
title.tag = 1001;
title.backgroundColor = [UIColor clearColor];
title.textColor = [UIColor blackColor];
title.text =[dealarray objectAtIndex:indexPath.row];
UILabel* detail;
detail= [[UILabel alloc] initWithFrame:CGRectMake(5,30,300,10)];
[cell.contentView addSubview:detail];
[cell.contentView bringSubviewToFront:detail];
[detail setFont:[UIFont systemFontOfSize:12]];
detail.tag = 1002;
detail.backgroundColor = [UIColor clearColor];
detail.textColor = [UIColor blackColor];
detail.text = [detailarray objectAtIndex:indexPath.row];
return cell
}
No problem in displaying those 2 labels and no problem in hiding all the 'detail' label and displaying the 'title' alone, the problem arises when I try to display the 'detail' label of the resp selective of cells.
Code tried:
// conti of cellforrowatindexpath
detail.numberOfLines = 3;
detail.lineBreakMode = NSLineBreakByWordWrapping;
if (a==-1)// declared 'a' in viewdidload as -1
{
((UILabel*)detail).hidden = YES;
}
else if(a==indexPath.row)
{
((UILabel*)detail).hidden = NO;
}
((UILabel*)detail).hidden = YES;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
a=indexPath.row;
[tableview reloadData];
}
Sorry for posting large amount of codes, it may help any one who is searching for the wholesome data of my doubt.
Whats the mistake am doing, I can't hide the detail label for resp selecting of cells. Can anybody help in this regard?
May I suggest you to use xcode functionnality to design your cell content? (as easy as drag and drop of UILabel on your cell for example) Then you will be able to access them from your code using their respective "tag" id.
A correct way of doing is detail here : http://www.appcoda.com/customize-table-view-cells-for-uitableview/
I think it's a better way than programmatically creating content for your cell, because using xcode interface you will be able to flawlessly define your interface.
Anyway to respond precisely to your question you should try to hide the detail label from "didSelectRowAtIndexPath" :
// Retrieve the corresponding cell
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
// Get the detail label (using its tag) you set it to 1002
UILabel *detail = (UILabel *)[cell viewWithTag:1002];
// Hide it
detail.hidden = true;
Hope this help.
There is an error in your logic. You code as written will always set detail.hidden to YES, the 2 preceding if are ignored, thus this (BTW you don't need the type coercion and extra brackets):
if(a==-1) detail.hidden = YES;
else if (a==indexPath.row) detail.hidden = NO;
else detail.hidden = YES;
Basically, I have a UITableView which will hold, say, alarms.
There is a '+' button at the bottom of the screen to add alarms but, of course, before any alarms are added the table is blank.
If there are no alarms, and the table view is empty, I'd like there to be some sort of placeholder text like "press the + to add an alarm".
I've tried this and also found a suggestion about making a placeholder UITableView with a cell that shows the above text. Then you show/hide the placeholder UITableView or the alarm UITableView depending on whether or not there are any alarms. I couldn't get that to work and think it's a bit much for wanting a simple string to show up.
I also tried creating a placeholder UITableViewCell if there are no alarms but that messes with numberOfRowsInSection which, in turn, breaks the ability to delete cells, as well it should (because numberOfRowsInSection can never be zero).
UPDATE: I also tried adding a UILabel to the tableView's tableHeaderView (and tableFooterView). No luck.
Any ideas?
After some researching on this site, I solved it by implementing the following delegate callbacks:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
if (self.alarmList.count == 0) {
UILabel *label = [[UILabel alloc] init];
label.text = #"press the + to add an alarm";
label.textAlignment = UITextAlignmentCenter;
label.numberOfLines = 2;
label.font = [UIFont boldSystemFontOfSize:16];
label.backgroundColor = [UIColor darkTextColor];
label.textColor = [UIColor whiteColor];
return label;
} else {
return nil;
}
}
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
if (self.alarmList.count == 0) {
return 68;
} else {
return 0;
}
}
I have a custom UITableViewCell. It has 3 custom labels inside it with custom text.
When i tap on the cell, i want the textColor of all those labels to go white. just like Email app UITableViewCell behavior.
For that, I wrote this in the custom cell class.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
if (self.selected) {
_subjectLabel.textColor = [UIColor whiteColor];
_messageLabel.textColor = [UIColor whiteColor];
_usernameLabel.textColor = [UIColor whiteColor];
}else {
_subjectLabel.textColor = [UIColor blackColor];
_messageLabel.textColor = [UIColor grayColor];
_usernameLabel.textColor = [UIColor blackColor];
}
}
I was able to get it. But its not as smooth as it is in the Email app. The color changes only after a small delay. Which method of UITableViewCell should I override to put this code in. I know about the below options, but they don't give the behavior onto custom labels in the custom cell.
typedef enum {
UITableViewCellSelectionStyleNone,
UITableViewCellSelectionStyleBlue,
UITableViewCellSelectionStyleGray
} UITableViewCellSelectionStyle;
Set the label's highlightedTextColor and this will all be done for you automatically. You should not have to do anything special in setSelected at all.
e.g.
_subjectLabel.highlightedTextColor = [UIColor whiteColor];
When we select any cell of UITableView , -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath method called immediately, you can use this.