When I build and run there is nothing displayed. No text just empty cells.
Here is my code:
#import "plungerselection.h"
#implementation plungerselection
#synthesize tableData;
-(void) viewDidLoad
{
tableData = [[NSArray alloc] initWithObjects:#"Turbo", #"Hollow Turbo", #"Single Pad", #"Dual Pad", #"Bullet", nil];
[super viewDidLoad];
}
#pragma mark - TableView Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"MyCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MyCell"];
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
}
return cell;
}
#end
Make sure you have wired up the datasource and delegate connections in Interface Builder.
You are only setting the text in the cell if dequeueReusableCellWithIdentifier: returns nil which in this case doesn't happen (and even if it did it will only happen a few times before all the cells that are needed get in the queue). You should set the text outside of the if (cell == nil)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
{
UITableViewCell *cell = nil;
cell = [tableView dequeueReusableCellWithIdentifier:#"MyCell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"MyCell"];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
Related
I have UITableView with prototype cell.I have a label in cells with various values(text value).
when add a new row, some of labels in new rows created with previous cell values not default values:
What can i do?
Is my question clear?
This is my code:
- (void)viewDidLoad
{
[super viewDidLoad];
gamers = [[NSMutableArray alloc] initWithObjects:#"Player1",#"Player2", nil];
}
- (IBAction)btnAddRow:(id)sender {
[gamers addObject:#"new player"];
[_tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *tableIdentifier = #"gamerCell";
nTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:tableIdentifier];
if (cell == nil) {
cell = [[nTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];
}
cell.lblTitle.text = [gamers objectAtIndex:indexPath.row];
return cell;
}
Please find the updated project in the link
UITableView-dynamic-row-buttons
Hope this helps.
Where do you update the score? if you are not doing, make, according to your logic, and then set this value in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *tableIdentifier = #"gamerCell";
nTableViewCell *cell = (nTableViewCell *)[tableView dequeueReusableCellWithIdentifier:tableIdentifier];
if (cell == nil) {
cell = [[nTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:tableIdentifier];
}
cell.lblTitle.text = [gamers objectAtIndex:indexPath.row];
cell.lblScore.text = [NSString stringWithFormat:#"%i", (int)scoreProperty];
return cell;
}
I have made customCell for my UITableView. I am downloading data from server and showing on CustomCells.
The method of manipulating data is
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"CustomCellReuseID";
LeaderBoardCell *cell = [leaderBoardTable dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[LeaderBoardCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
GetScoreBoard *ob = [leaderBoardArray objectAtIndex:indexPath.row];
[[cell title] setText:ob.fullname];
[[cell discription] setText:ob.points];
return cell;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [leaderBoardArray count];
}
The CustomCell "LeaderBoardCell" is loading later, but before this I see simple cells which are default in UITableView.
Is is possible to load only CustomCell in TableVeiw?
have a UITableViewCell "postCell" that is an IBOutlet and is attached to a cell I have placed in my .xib. To set the text label:
- (void)viewDidAppear:(BOOL)animated
{
postCell.textLabel.text = #"TEXT";
}
Nothing ever appears.
Edit: This cell is not part of a Table View. Every answer I have seen is assuming it is. This is a single UITableViewCell.
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
PostCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[PostCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = #"text";
return cell;
}
You can set text label for the cell in method cellForRowAtIndexPath, please see example below.
- (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];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.textLabel.text = #"Cell Title";
return cell;
}
There are a few delegate methods that you need to implement. Here are the two most important ones
tableView:cellForRowAtIndexPath:
tableView:numberOfRowsInSection:
Here's a full tutorial on how to implement a table view.
http://www.raywenderlich.com/?p=1797
i'm trying to do a selection list in UITableView but whenever i tap in a row, two row are marked. For example if i tap the first row it adds a checkmark to row = 0 and row = 11. I think the problem is caused by reuse of cells but i don't understand how solve it.
I attach my code .h:
#interface tabellaViewController : UITableViewController
#property (nonatomic, strong) NSArray *listaingredienti;
#end
and my code .m:
- (void)viewDidLoad
{
[super viewDidLoad];
_listaingredienti = [NSArray arrayWithObjects:#"formaggio", #"crudo", #"pomodorini", #"fossa", #"bresaola", #"funghi", #"salame", #"fontina", #"rucola", #"radicchio", #"peperoni", #"ciccioli", #"squacquerone", #"gorgonzola", #"salsiccia", #"cipolla", #"feta", #"acciughe", #"pecorino", nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_listaingredienti count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [_listaingredienti objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow]
animated:NO];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
I've seen UITableView didSelectRowAtIndexPath add additional checkmark at tap where there is a similar post but i already use dequeueReusableCellWithIdentifier:CellIdentifier.
Anyone can help me?
Ok,
i'm just solved my question. Actually, the problem was in reuse of cells, but this code fix it.
Just replace in cellForRowAtIndexPath::
static NSString *CellIdentifier = #"Cell";
with:
NSString *CellIdentifier = [NSString stringWithFormat:#"MyReuseIdentifier %d",indexPath.row];
I get an array of another class, and make changes to this class, but when I do a reloadData not much happens.
What is wrong?
This is my code
#pragma mark TesteFaceCarViewController methods
-(void)valores:(NSMutableArray *)array
{
arrayInfracao = array;
NSLog(#"Quantidade :%d", arrayInfracao.count);
[table reloadData];
}
#pragma mark TableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSLog(#"Passei aqui!");
NSLog(#"Quantidade :%d", arrayInfracao.count);
return arrayInfracao.count;
}
-(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];
}
Infracao *infracao = [[Infracao alloc] init];
infracao = [arrayInfracao objectAtIndex:indexPath.row];
cell.textLabel.text = infracao.dsInfracao;
return cell;
}
You are welcome!
[self.table reloadData];