Okay so I have a view controller with about 5 UITableView Cells inside it. They are all static. What i want to do is click on "Cell One" and then load the "Cell One" PDF. Right now I have it set up with segues in my storyboard so that if you click "Cell One" or "Cell Two" they all open the same PDF. That PDF is loaded in another UIWebView view although i dont know if that matters. So basically can somone help me figure out how I can get the title of the cell that was clicked? Thank you!
In your didSelectRowAtIndex: method you want to do this:
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *cellTitle = [[cell textLabel] text];
Now you're cell title will be in the cellTitle NSString.
This assumes you haven't used a custom cell. If you have then you just ref your custom label i.e.
YOURCUSTOMCELLCLASS *cell = (YOURCUSTOMCELLCLASS *)[tableView cellForRowAtIndexPath:indexPath];
NSString *cellTitle = [[cell CUSTOMLABEL] text];
Related
I am trying to set the subtitle label on a UITableViewCell. I have a dictionary containing the pairs of titles and subtitles and the list of keys is stored in self.results. However when I run with the following code:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Search Result Cell" forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Search Result Cell"];
}
cell.textLabel.text = [self.results objectAtIndex:indexPath.row];
cell.detailTextLabel.text = self.resultPairs[[self.results objectAtIndex:indexPath.row]];
NSLog(#"%#", self.resultPairs[cell.textLabel.text]);
The subtitle remains blank, but the NSLog prints the correct output. What is going on here? When I set the detailTextLabel to a constant it works fine.
The problem is that the default cell style does not include a subtitle. If you are using a storyboard, you just need to set the style to Subtitle in Interface Builder.
The solution turned out to be that the strings contained allot of initial whitespace, resulting in them being padded out of the view.
I have a UIViewController that contains a UITableView. In my table view I have a prototype cell in which I want to place custom labels. I cannot get any of my custom labels to render in the cell. The datasource and tableview delegates are connected and functioning properly. For my first attempt, I dragged a UILabel control onto my prototype cell. I set its tag to 1000. Here is my code to populate the cells:
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellID = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
UILabel *customLbl = (UILabel*)[cell viewWithTag:1000];
if(indexPath.section == 0){
[cell.textLabel setText:[rData.currentHeightReadings objectForKey:[heightKeys objectAtIndex:indexPath.row]]];
[customLbl setText:#"CFS"];
} else {
[cell.textLabel setText:[rData.currentFlowReadings objectForKey:[flowKeys objectAtIndex:indexPath.row]]];
[customLbl setText:#"Ft."];
}
return cell;
}
The default textLabel for each cell renders as it should. However, the custom label does not appear. I verified that the prototype cell was actually rendering by changing the background color of the cell's content view. In my story board I have set the prototype cell's reuse identifier to match my code. The style is set to 'Custom.' What else should I be doing? Thanks!
Verify that your autolayout constraints are correct such that the UILabel is really positioned where you expect it.
I had the same issue once and I've solved it by not calling any methods to cell.textLabel property. Just don't set its text in any way.
Try it out and report back.
Try this:
UILabel *customLbl = (UILabel*)[cell.contentView viewWithTag:1000];
I have a dynamic TableView that looks like this.
However, I'd like to position the text more neatly, like this.
Here's the code I'm currently using:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =#"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[cell.textLabel setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:17.0f]];
cell.textLabel.text=[[self.responseArray objectAtIndex:indexPath.row]objectForKey:#"name"];
return cell;
}
I've experimented with putting a label into the TableViewCell that's positioned where I want the dynamic text to be, but I've been unsuccessful in calling it in my code. How can I do this?
You should create a custom cell subclass. This subclass should have an #property in the .h file which makes your custom label available. In the .m (or XIB) you create (or connect) the label to the property (outlet).
Now, instead of using cell.textLabel you use cell.customTextLabel. Just remember to register the custom cell class against your CellIdentifier.
You can also configure the font and size on the label in the cell .m (or XIB) so you don't need to do it in code.
You will also want to change to:
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
so that the compiler knows that class the cell is (and thus what properties it has).
There is no need to create subclass of UITableViewCell. Coz, you want just change origin of your text.
I recommend to add a UILabel to your cell as subview.
cell.textLabel.text = #"";
UILabel *textLabel = [[UILabel alloc] initWithFrame: CGRectMake(10,10,300,20)];
textLabel.text = #"Text here";
[cell.contentView addSubview:textLabel];
2-3 lines only and you needn't create a subclass.
I have a static UITableView to which I am populating through NSDictionary in cellForRowAtIndexPath method.
NSString *key = [sectionKeys objectAtIndex:[indexPath section]];
NSArray *contents = [sectionContents objectForKey:key];
NSString *contentForThisRow = [contents objectAtIndex:[indexPath row]];
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
[[cell textLabel] setText:contentForThisRow];
Thinks working well, I also need to add controls to tableviewcells and every cell has different controls. When I programmatically add UITextField to cell1 the view after running is fine till 5 sections, but when I scroll my tableview further down, the cell of section 6 also gets a UITextField control. Same is happening with UIDatePicker, that first 5 sections of my tableview are fine but on scrolling the tableview the last two sections also get the UIDatePicker control.
Note: I have nine sections in table view each having one cell with the exception of first two sections that contain two cells.
What I understood:
The layout of your cells is getting messed up and controls from one cell go to the other one.
What I suggest:
It is a common problem, I faced it a while ago and it was extremely frustrating. After a few hours of googling I found a solution: Use different cell identifiers for cells with different controls.
Example:
5 cells: 2 cells with only a UITextField, 1 cell with UITextField and a UILabel and 2 cells with UIImageView. Use 3 different cell identifiers. 1 for the cells with only a UITextField, 1 for the cells with UIImageView and 1 for the cell with UITextField and a UILabel.
Hope it helps.
is there a way I can have in the search results controller table view the exact same styling (height, background etc) I have in the prototype cell of my tableview controller in iOS5?
The root view has a background picture and a specific cell height. The search table does not appear with the background picture and the cell height is lower;
Another way is to just use the same cell identifier (at least if you're using storyboards), so for example:
static NSString *CellIdentifier = #"searchCell";
myCustomCell *cell = (myCustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//for search controller need to check if cell is nil, if it is create one since there won't be any to reuse to begin with
if (!cell) {
cell = (myCustomCell *)[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
Other properties such as row heights etc can be set by accessing properties of
self.searchDisplayController.searchResultsTableView