I have a view controller that shows up a table view controller when a button is clicked. Everything works fine with table view delegates, table view is shown fine, but in ellForRowAtIndexPath: delegate method, cell is instantiated and returned, but it's not shown correctly.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"alrededor";
alrededorCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[alrededorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *categoria = [[NSDictionary alloc] initWithDictionary: [_categoriasArray objectAtIndex:indexPath.row]];
NSLog(#"categoria %#", categoria);
cell.title.text = [categoria valueForKey:#"title"];
return cell;
}
Many thanks
Why are you creating your cell like this?
if (cell == nil) {
cell = [[alrededorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
If it is your own custom cell why do you use UITableViewCellStyleDefault ?
If the cell you are loading is a subclass of UITableViewCell and you've used the interface builder to build the cell you have to do a couple of things.
In the nib file delete the view that is there when you create it, add a UITableViewCell and change it's class to alrededorCell. Changes the cells class and not the file's owner. When you are linking buttons,labels etc . be sure to link them to the cell not to file's owner. Also set the cells uniqueIdentifier to alrededor.
In cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"alrededor";
alrededorCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray *xib = [[NSBundle mainBundle] loadNibNamed:#"nibName" owner:nil options:nil];
for (alrededorCell *view in xib) {
if ([view isKindOfClass:[alrededorCell class]]) {
cell = view;
}
}
}
NSDictionary *categoria = [[NSDictionary alloc] initWithDictionary: [_categoriasArray objectAtIndex:indexPath.row]];
NSLog(#"categoria %#", categoria);
cell.title.text = [categoria valueForKey:#"title"];
return cell;
}
Related
I've created a cell with 2 UIButtons in the IB and subclassed it. How can I use it without reusing it over again? (for a small fixed table)
I tried doing something like:
RaffleResultCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
but that won't show my cell in the UITableView, just a blank one.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//Where we configure the cell in each row
id currentRaffle = [_winnings objectAtIndex:indexPath.row];
RaffleResultCell *cell = [tableView dequeueReusableCellWithIdentifier:#"raffleResCell"];
if (cell == nil) {
cell = [[RaffleResultCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"raffleResCell"];
}
return cell;
}
It is not a good practice to avoid Reusability,I will say dont do it
Reusability is done in this line
RaffleResultCell *cell = [tableView dequeueReusableCellWithIdentifier:#"raffleResCell"];
Remove that line and just call alloc method everytime without the check loop
Like
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
id currentRaffle = [_winnings objectAtIndex:indexPath.row];
cell = [[RaffleResultCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"raffleResCell"];
return cell;
}
You told you set up the UITableViewCell in IB then you need to get Nib file and then use that file as
// Get nib file from mainBundle
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:#"RaffleResultCell" owner:self options:nil];
for (id currentObject in topLevelObjects) {
if ([currentObject isKindOfClass:[UITableViewCell class]]) {
RaffleResultCell *cell = (RaffleResultCell *)currentObject;
break;
}
}
now setup any text for your button and return cell
I have a UITableView within a View Controller. Very strange because the header is showing up and the header text but not the cell text.
- (void)viewDidLoad
{
table.delegate = self;
selection= [[NSMutableArray alloc] init];
[selection addObject:#"text"];
[selection addObject:#"text"];
[selection addObject:#"text"];
...}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// configure the cell in each row
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]autorelease];
}
NSString *cellValue = [selection objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
// cell.textColor=[UIColor blackColor];
return cell;
}
Try adding this line
table.dataSource = self;
This way, all the table view data source methods (including cellForRowAtIndexPath:) are called, so long as your class conforms to the UITableViewDataSource protocol.
I need to display each webview in each of the tableviewcell in my Uitableview.When using the below code,when there are 2 elements,the first cell is empty but second cell is correct.hrs and hrsHtml contains all values,the problem is only the last data is displaying in their appropriate cell in tableview.Other cells are just blank.Also is total cells is 2,first we can only been able to see the 2nd cell only,but after scrolling tableview reloads and 2nd cell disappears and 1st cell gets displayed.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [brId count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
if (cell == nil) {
cell = [[ UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell.contentView addSubview:hrs];
hrsHtml = [NSString stringWithFormat:#" <font size=\"2\" face=\"Arial\">%# </font>",[html objectAtIndex:indexPath.row]];
[hrs loadHTMLString:hrsHtml baseURL:nil];
return cell;
}
Screenshot when tableview appears,Only webview in cell 2
Screenshot when tableview scrolls,Only webview in cell 1,cell 2 disappears
Since hrs and hrsHtml are single objects, you only have one of each even though you have multiple cells. If you modify hrs, it will change for all cells since they seem to be sharing it. (Unless you have other code somewhere that changes the objects that those variables point to.)
Another odd thing is that you use a brId array to determine the number of rows and a html array to get the row content. If those ever get out of synchronization, you will have problems.
Also, you should only add a subview to a cell when you create a new one.
- (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:nil] ;
}
hrs = [[UIWebView alloc] initWithFrame:CGRectMake(10,0,320,84)];
hrs.userInteractionEnabled = YES;
hrs.backgroundColor = [UIColor clearColor];
hrsHtml = [NSString stringWithFormat:#" <font size=\"2\" face=\"Arial\">%# </font>",[html objectAtIndex:indexPath.row]];
[hrs loadHTMLString:hrsHtml baseURL:nil];
[cell.contentView addSubview:hrs];
hrsHtml = nil;
return cell;
}
Now the webview loads correctly in every tableviewcell.
i think you have problem in initializing cell try below code
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
}
A small issue i created a custom UITablecell but in the cell it's need to parse data so i connected IBOutlet UILabel *One; to an UILabel but when i'm doing
One.text = #"Lorem..."; an error show's up i imported the UITablecell.h in mijn viewController.
**Use of undeclared identifier 'One'**
/
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
ViewControllerCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSArray* views = [[NSBundle mainBundle] loadNibNamed:#"ViewControllerCell" owner:nil options:nil];
for (UIView *view in views) {
if([view isKindOfClass:[UITableViewCell class]])
{
cell = (ViewControllerCell*)view;
}
}
}
One.text = #"Lorem...";
return cell;
}
In this case your instance of your custom UITableViewCell class will be cell, so you'll need to access it like this
cell.One.text = #"Lorem..";
First you have to Typecast the tableViewCell to your Custom Cell.
{
YourCustomCell *objCustomCell=(YourCustomCell *)[tableView cellForRowAtIndexPath:indexPathForThatRow];
objCustomCell.One.text=#"YourDesiredstringvalue";
}
So I'm using mogenerator with Core Data, the resultsController returns good objects with valid attributes when I first load the tableview. But when I scroll the table, all the reloaded cells are populated with objects with null attributes returned from resultsController. Is it some caching issue?
- (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];
}
// Configure the cell...
Log *log = [self.resultsController objectAtIndexPath:indexPath];
cell.textLabel.text = [NSString stringWithFormat:#"%#", log.text];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
Turns out I shouldn't put the resultsController in the viewWillLoad or viewDidLoad. I put it into it's own access method and it works fine now.