I am trying to query images in my UITableview, but I am having a problem trying to configure the cell.
Create a subclass of UITableViewCell and add a UIImageView property to it. Here's an example of what your header file would look like:
#import <UIKit/UIKit.h>
#interface CustomTableViewCell : UITableViewCell
#property (nonatomic, weak) IBOutlet UIImageView *icon;
//#property (nonatomic, strong) UIImageView *icon; //Only if you're not using an XIB/storyboard
#end
Then, your cellForRowAtIndexPath: function would look something similar to this:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=#"cellimage";
CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
//the following assumes that your self.images array contains UIImages
cell.icon.image = [self.images objectAtIndex:indexPath.row];
return cell;
}
Also, be sure to call reloadData on the main thread.
Related
I have noticed that UITableViewCell is not reusing it's subviews and seems to be released when scrolling up and down. I am creating UITableViewCell subview and animating it on viewDidAppear. I want to do this only once and then I expect subview to stay in the ContentView forever until view disappears. So I have done following which I think right way but not sure what's wrong; can someone help?
UINib *nib1 = [UINib nibWithNibName:#"CustomCell" bundle:nil];
[self.tableView registerNib:nib1 forCellReuseIdentifier:#"CustomCell"];
#interface CustomCell : UITableViewCell
#property (nonatomic) IBOutlet UILabel *title;
#property (nonatomic) CustomeView *customeView;
#end
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString cellIdentifier = #"CustomCell";
CustomCell *customCell = [tableView dequeueReusableCellWithIdentifier:accountCellIdentifier forIndexPath:indexPath];
......
customCell.customeView = (CustomeView*)[customCell.contentView viewWithTag:101];
if(!customCell.customeView) {
customCell.customeView = [CustomeView new];
//configure customview...
customCell.customeView.tag = 101;
[customCell.contentView addSubview:customCell.customeView];
}
return cell;
}
Try to change your property type to strong:
#property (strong, nonatomic) CustomeView *customeView;
I used to work like this:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryNone;
}
But now I have to do the same thing on a cell that is not a UITableViewCell:
I had to put in cells a lot of stuff, so i couldn't use normal UITableViewCells, and I made it my own (called PrototypeTableViewCell). Like this:
#interface PrototypeTableViewCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UILabel *eventTitle;
#property (weak, nonatomic) IBOutlet UILabel *eventLocation;
#property (weak, nonatomic) IBOutlet UILabel *eventStartDate;
#property (weak, nonatomic) IBOutlet UILabel *eventEndDate;
#end
So how can I do the things I used to do on normal TableViewCells (like setting the accessory:checkmark)?
"cellForRowAtIndexPath:indexPath" won't work with the class I created.
Your PrototypeTableViewCell is a subclass of UITableViewCell. All you need to do is simply cast UITableViewCell to your PrototypeTableViewCell as follows:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// cast UITableViewCell to your PrototypeTableViewCell
PrototypeTableViewCell *cell = (PrototypeTableViewCell*)[tableView cellForRowAtIndexPath:indexPath];
// use its properties as normal
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
I made a subclass of UITableViewCell to get a bigger TableViewCell with some more options.
But my problem is that I can't set the text(s) of the label(s):
BlogItem *bi = [[channel items] objectAtIndex:[indexPath row]];
NSLog(#"%#", [bi title]);
[[cell mainLabel] setText:[bi title]];
NSLog(#"%#", [[cell mainLabel] text]);
The first log message returns the text I expected, but the second one always logs (null).
I really don't know what should be wrong. I've created the labels as usual:
#property (weak, nonatomic) IBOutlet UILabel *mainLabel;
Of course I connected the labels and synthesized them (checked it twice). I've also implemented the method
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
to get the appropriate height for each cell (which works fine).
BTW, checkmarks appear as expected. It's just about the labels.
Make sure you are instantiating your custom UITableViewCell subclass in the cellForRowAtIndexPath method. Also make sure that those IBOutlets are declared in your UITableView subclass and NOT in the View Controller that houses the TableView. Also make sure that the parent class of your cell in interface builder is set to that same subclass.
Something like this (Custom UITableViewCell subclass interface file):
#import <UIKit/UIKit.h>
#interface MyCustomCell : UITableViewCell
#property (nonatomic, weak) IBOutlet UILabel *mainLabel;
#end
Then #synthesize in the implementation file:
#synthesize mainLabel;
Then in cellForRowAtIndexPath, something like:
static NSString *CellIdentifier = #"MyCellIdentifier";
MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
BlogItem *bi = [[channel items] objectAtIndex:[indexPath row]];
// Configure the cell...
cell.mainLabel.text = [bi title];
// ... Other stuff
return cell;
I have a class named FirstViewController which is a subclass of UIViewController and I have a UITableView in the class. I need to transition from this class to a new UIViewController subclass (using segues) when a row is pressed.
I have made the FirstViewController the UITableViewDelegate and UITableViewDataSource using the code -
#interface FirstViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
I am getting an error on this line of code -
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
How do I fix this problem since the tableView is found in UITableViewController and not in UIViewController class ?
Edit -
Here's the code related to my table view -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [sarray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellId = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
if(cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellId];
}
NSString *text = [sarray objectAtIndex:[indexPath row]];
cell.textLabel.text = text;
return cell;
}
You can simply declare tableView as a property:
#property(nonatomic, strong) IBOutlet UITableView *tableView;
You get a free UITableView only if you subclass UITableViewController,
#interface FirstViewController : UITableViewController
you can simply declare a table view property on FirstViewController using:
#property(nonatomic, strong) IBOutlet UITableView *tableView;
you then need to connect this property to the Table View in the story board.
I've got a question on how to add a UIView to a UITableViewCell.
In my learning Project I've got a UITableViewController (TableTestViewController, a UITabelViewCell.xib (MainTabelCell.xib) and a UIView (InnerTableView).
My Goal ist to show the MainTableCell in the UITableView and in this MainTabelCell the InnerTabelView but I only manage to show the Cell in the Table.
TableTestViewController.h:
#import <UIKit/UIKit.h>
#import "InnerTableView.h"
#interface TableTestViewController : UITableViewController {
UITableView *mainTable;
UITableViewCell *nibLoadedCell;
InnerTableView *innerView;
}
#property(nonatomic, retain) IBOutlet UITableView *mainTable;
#property(nonatomic, retain) IBOutlet UITableViewCell *nibLoadedCell;
#property(nonatomic, retain) InnerTableView *innerView;
#end
TableTestViewController.m:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
[[NSBundle mainBundle] loadNibNamed:#"MainTableCell"
owner:self options:NULL];
cell = nibLoadedCell;
}
// Configure the cell.
innerView = [[UIViewController alloc] init];
[cell addSubview: innerView.view];
return cell;
}
Does anyone have an Idea why the innerView will not be shown in the TabelViewCell? What did I miss here? I'm still learning all that stuff but after 5 hours I don't find a clue in my Books. Hope this is not a to dumb question :)
Thank you
CaptnCrash
You should use [cell.contentView addSubview:yourView];
BTW, in your code, your innerView is not released properly.