XCode Reusing UITableViewCells in Storyboard - xcode

I have a UITableViewCell in my project that I want to reuse in a few UITableViews. Is there a way to reuse them without setting up UILabels, etc each time in the Storyboard?

In cellForRowAtIndex, you can refer to the custom cell:
- (UITableViewCell *)tableView (UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
CustomCell *cell = ...
cell.labelName.text = ...;
Return cell;
}

You need to create your UITableViewCell class and register it for your tables
- (void)viewDidLoad
{
[super viewDidLoad];
[self.tableView registerClass:<RegisteredClass> forCellReuseIdentifier:<ReuseIdentifier>];
}

Related

How to hide a static UITableViewCell with a button in Swift

Hi I have a table view with static cells. I want to hide specific ones with a button. I must hide them according to the current time of the iphone so i created UITableViewCell outlets for each of them. How can i hide a cell when the button is clicked ? Thanks in advance.
I get what you want to do. if you want to hide action, you need to control your static tableView.
Don forget to reload tableview in your action.
[self.tableView reloadData];
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row == 0) {
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
cell.hidden=YES;
return 0;
}else{
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
cell.hidden=NO;
return [super tableView:tableView heightForRowAtIndexPath:indexPath];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell;
cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
return cell;
}

populate table view with prototype cells

I have this scene:
I added one simple "table view" and next +1 into "prototype cells":
informacao.h
NSMutableArray *arrCursos;
informacao.m
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Informações";
arrCursos = [[NSMutableArray alloc] init];
[arrCursos addObject:#"[G] - Odontologia"];
[arrCursos addObject:#"[P/LT] - Odontologia"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [arrCursos count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cursosCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [arrCursos objectAtIndex:indexPath.row];
return cell;
}
I don't know why my table view is empty, like this:
Now, there are a few places you should check. First is whether the table view data source has been connected to the view controller. Check if the number of rows data source method is called and whether it is returning the expected number of rows.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"%d", arrCursos.count);
return [arrCursos count];
}
Second is to check whether you have specified the cell identifier cursosCell in your storyboard.
the problem was missing delegate and datasource

Outlet in a Static Table Cell Causes EXC_BAD_ACCESS on load

I am creating a simple Static Table with 2 sections, 2 Cells in each section. the Table View controller (TVC) is Subclassed.
Within each of the table cells I have a stepper Control, and and a label. I am using this to allow for updating though an IBAction from the Stepper, setting a property in the TVC. At the same time the label is updated through it's outlet to show the current value.
The outlet is defined in the #interface section of the "TVC.m" file as is normal using the standard IB tools.
On run it breaks on the Thread 1 level under UIApplicationMain in something named _0 obj_loadWeakRetained_ and the error is:
THREAD 1: EXC_BAD_ACCESS(code -2, address = 0x1da7cc0.
Any idea what I may be doing incorrectly? is there something that has to be done differently when adding views to tablecells and setting outlets to them?
Here's the code - nothing unusual.
#import "AdvancedTVC.h"
#interface AdvancedTVC ()
- (IBAction)stepperChanged:(UIStepper *)sender;
- (IBAction)switchToggled:(UISwitch *)sender;
#end
#implementation AdvancedTVC
- (IBAction)stepperChanged:(UIStepper *)sender {
}
- (IBAction)switchToggled:(UISwitch *)sender {
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
// Configure the cell...
return cell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}
#end

cellForRowAtIndexPath not called

I have a Master-Detail application. When the user selects a row in the Master table(MasterViewController),I want to open another UITableViewController (DocumentViewController) and display data here(not in the Detail view).
When I run the application,"numberOfSectionsInTableView" for DocumentViewController is called but "cellForRowAtIndexPath" is not called.
The code in DocumentViewController.m is:
- (void)viewDidLoad
{
[super viewDidLoad];
[docTable setDataSource:self];
[docTable setDelegate:self];
EDViPadDocSyncService *service = [[EDViPadDocSyncService alloc]init];
EDVCategory *cat = [EDVCategory alloc];
cat.categoryId = [catId intValue];
[service getDocsByCatId:self action:#selector(getDocsByCatIdHandler:) category:cat];
[self.docTable reloadData];
}
- (void) getDocsByCatIdHandler: (id)value {
//snip......
[docTable reloadData];
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.myDocList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
DocumentCell *cell = [tableView dequeueReusableCellWithIdentifier:#"DocumentCell"];
if (cell == nil)
{
cell = [[[DocumentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"DocumentCell"] autorelease];
}
NSLog(#"cell text=%#",[self.myDocList objectAtIndex:indexPath.row]);
cell.lblDocName.text = [self.myDocList objectAtIndex:indexPath.row];
return cell;
}
Thanks for the help.
EDIT:- I have set the delegate and datasource properties for the table.The table is declared as "#property (nonatomic,retain) IBOutlet UITableView *docTable;" in the DocumentViewController.h. I have tried using 'viewwillappear' as well as 'initwithstyle' but these doesn't seem to work either.

Xcode bug? VoiceOver with TableView

I was just wondering is this an existing bug OR is there anyway to fix this. When in tableview, turning on voiceover will result in empty table cells... anyone?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"tableCell";
//Exisint bug if using TableViewCell
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell.
cell.pLabel.text = [self.tableArrayPlaces objectAtIndex:[indexPath row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedDest = [tableArrayPlaces objectAtIndex:indexPath.row];
//Get the selected country
NSLog(#"SelectedDest %#", selectedDest);
}
Edited: Codes are added. This is my codes for populating my cells. In storyboard I set it to "push", can't seem to work. Any help?
It might be a bug in iOS or in your code, but it's probably not a bug in Xcode.
Check your table view controller's -tableView:cellForRowAtIndexPath: method. Is it being called when the empty cells are generated? If no, why not? If yes, is it returning properly formatted cells? Can you reproduce the problem in other applications such as Contacts?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"tableCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell.
cell.text = [tableArrayPlaces objectAtIndex:[indexPath row]];
return cell;
}
Instead of using my own cellView, I used UITableViewCell. This solves my problem. But of course I can no longer do any linking at my storyboard directly.

Resources