Adding to the UITableView on UIButton - ios

At one ViewController is located separately UITableView, UITextField and UIButton. I need to add UITextField's text to the cell of the table by button. I set the flag on the pressing process:
- (IBAction)addingButon:(id)sender {
_addingFlag = true;
}
And then in the method of the table do the following:
- (UITableViewCell *)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = #"dataCell";
UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:cellID];
while (true) {
if (_addingFlag && _textBox.text.length != 0) {
cell.textLabel.text = _textBox.text;
[tableView reloadData];
}
_addingFlag = false;
break;
return cell;
}
Data in table appears only when scrolling and in the strange order. Can you please tell how to fix it. Working with Objective-C just a couple of days.

tableView:cellForRowAtIndexPath: should return immediately. Instead, call [tableView reloadData] in addingButton::
- (IBAction)addingButon:(id)sender {
[self.tableView reloadData];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID = #"dataCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
cell.textLabel.text = _textBox.text;
return cell;
}

Related

How to hide a particular row button in Dynamic UITableViewCell in Objective C?

I have implemented a Dynamic TableView, Textfield and Buttons. My problem is, when I hide the button in the first row of my UITableViewCell, the other five rows of cell button also get hidden.
Can any one suggest a solution for this issue?
I have tried below code..
ladiesdetails=[[NSMutableArray alloc]initWithObjects:#"2",#"0",#"0",#"0",#"0",#"0", nil];
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"cell1";
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[passengerdetailcell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
if([[ladiesdetails objectAtIndex:indexPath.row] intValue]==2)
{
cell.malebutton.hidden=yes;
}
return cell;
}
Just put the else condition and make the button visible in cellForRowAtIndexPath method. If you have any other condition to show add that as well.
if([[ladiesdetails objectAtIndex:indexPath.row] intValue] == 2) {
cell.malebutton.hidden = YES;
} else {
cell.malebutton.hidden = NO;
}
like this
bool flag = ([[ladiesdetails objectAtIndex:indexPath.row] intValue] == 2)
cell.malebutton.hidden = flag
It happens because of cell reuse , you use allocate the cell like below one .
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
PassengerDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"PassengerDetailCell" forIndexPath:indexPath];
if([[self.ladiesdetails objectAtIndex:indexPath.row] intValue]==2)
{
cell.maleButton.hidden = TRUE;
}
return cell;
}

IOS: UITableView have to double click to deselect

I have a UITableView that holds a tableView that populates 'prototype cells' from a mutable array and whose cells show an 'accessory checkmark' when you select them. I have a textfield in a view below the tableview and whose data I am then appending to the array that is populating the tableview. My problem is that after I append the new data which adds a cell to the tableview I have to touch a cell twice in order to deselect any of the cells I previously selected.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"skillName" forIndexPath:indexPath];
cell.textLabel.text = skillsOptions[indexPath.row];
// Configure the cell...
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
//tableViewCell.accessoryView.hidden = NO;
tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
//tableViewCell.accessoryView.hidden = YES;
tableViewCell.accessoryType = UITableViewCellAccessoryNone;
[skillsList removeObject:skillsOptions[indexPath.row]];
}
-(void)grabSelectedSkills {
for (NSIndexPath *indexPath in self.tableView.indexPathsForSelectedRows) {
NSString *skill = skillsOptions[indexPath.row];
//NSString *skill = [NSString stringWithFormat:#"%li",(long)indexPath.row];
[skillsList addObject:skill];
}
NSLog(#"skillsList: %#",skillsList);
}
- (IBAction)continue:(id)sender {
[self grabSelectedSkills];
}
- (IBAction)addOtherSkills:(id)sender {
if (self.otherSkill.text.length > 1) {
[skillsOptions addObject:self.otherSkill.text];
self.otherSkill.text = #"";
[self.tableView endEditing:YES];
[self.tableView reloadData];
}
You need to track your selection/deselection status outside of the cell - a dictionary keyed by the "skill option" is probably a good choice.
You can then use this in cellForRowAtIndexPath and didSelectRowAtIndexPath to add/remove the check mark as required. You should deselect the row in didSelectRowAtIndexPath and get rid of didDeselectRowAtIndexPath -
Create self.selectedCells as an NSMutableDictionary property.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"skillName" forIndexPath:indexPath];
// Configure the cell...
NSString *skillOption=skillsOptions[indexPath.row];
cell.textLabel.text = skillOption;
if (self.selectedCells[skillOption]!= nil) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *skillOption=skillsOptions[indexPath.row];
if (self.selectedCells[skillOption]!= nil) {
tableViewCell.accessoryType = UITableViewCellAccessoryNone;
[self.selectedCells removeObjectForKey:skillOption];
} else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
self.selectedCells[skillOption]=skillOption;
}
[tableview deselectRowAtIndexPath:indexPath];
}
I think you should only use didSelectRowAtIndexPath: method (drop the didDeselectRowAtIndexPath: method) and check to select the cell if it's not and vice versa as-
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BOOL cellSelected = NO;
// set cellSelected based on your condition
if(cellSelected)
{
// code to deselect the cell
}
else
{
// code to select the cell
UITableViewCell *tableViewCell = [tableView cellForRowAtIndexPath:indexPath];
//tableViewCell.accessoryView.hidden = NO;
tableViewCell.accessoryType = UITableViewCellAccessoryCheckmark;
}
}

iOS UITableView contents not visible

In a VC I have 2 tableViews - 1 main and other is added in a drop down menu. From drop down menu TableView "ddTableView", I have added 5 cells in Storyboard itself as prototype cells. Each cell contains an image and a Label. Have also set identifier for each cell. Have also set Accessory Type as "Disclosure Indicator" for each cell of ddTableView.
DataSource and Delegate for mainTV is set to the VC and delegate for ddTableView is set the VC. As the rows are added within the storyboard, I didn't set any datasource for ddTableView in my VC. I managed my delegate methods like this :-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == ddTableView) {
// RETURNS NIL ???
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(#"CELL Identifier = %#", cell.reuseIdentifier);
return cell;
} else {
}
}
-(void )tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == ddTableView) {
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog (#" CELL Clicked of - %#", cell.reuseIdentifier);
}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == ddTableView)
return 44;
else
return 60;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (tableView == ddTableView)
return 5;
else
return [chatMsgsArr count];
}
It does call respective delegate method. For cellForRowAtIndexPath it returns cell as nil. Why so ? And on execution, I don't see any contents of the table view - table is blank. I changed the bg color of image and text color also, but yet nothing is seen on execution.
Why so ? Where I may be going wrong ?
You have to allocate the cell, the first time cellForRowAtIndexPath is called. If the cell is not allocated it would return nil as you are getting right now.
What you need to do is allocate the cell only once, the first time you enter cellForRowAtIndexPath. After that you can reuse it as you have done.
If you are planning to use the default tableViewCell your method should look as follows:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"your_cell_identifier_name";
if (cell == nil)
{
UITableViewCell *cell = [UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
else
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
}
//write the code to populate your cell with data here
//return the cell object
return cell;
}
Hope this helps! :)
- (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] ;
}
if (tableView == ddTableView) {
NSLog(#"CELL Identifier = %d", indexpath.row);
} else {
NSLog(#"CELL Identifier = %#", [chatMsgsArr objectAtIndex:indexPath.row]);
}
return cell;
}

two row's are marked by one tap in UITableView selection list

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];

Doesn't work UITableViewCell's method

When I do simple HelloWorld app I have problem with
[self.tableView insertRowsAtIndexPaths:#[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic]
,this method doesn't work... and i don't know why? pliz help
Code:
- (void)viewDidLoad
{
int i = 0;
[super viewDidLoad];
while (i < 10 ) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
i++;
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.textLabel.text = #"HelloWorld";
return cell;
}
App should create 10 cells with Label #"HelloWorld"
You're going about this the wrong way. If you want 10 cells you shouldn't be trying to add them one at a time, you should just return 10 in numberOfRowsInSection, e.x:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 10;
}
Additionally, nothing will show up in the cells because you have call alloc/init on the cells in cellForRowAtIndexPath. Modify your code to do so like this:
- (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 = #"HelloWorld";
return cell;
}

Resources