I'm trying to make an editable section of rows using a UITableView
For some reason, my data does not load into the cell, and remains nil. I have been unable to figure out why, could someone give me some help ?
#interface exampleOrderCell : PSTableCell <UITableViewDataSource, UITableViewDelegate>
#property (nonatomic, strong) NSMutableArray *tableData;
#end
#implementation exampleOrderCell
//- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier specifier:(PSSpecifier *)specifier
- (instancetype)initWithStyle:(UITableViewStyle)style
{
return self;
}
- (void)viewDidLoad {
[self viewDidLoad];
self.tableData = [#[#"One",#"Two",#"Three",#"Four",#"Five"] mutableCopy];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ThisCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
cell.textLabel.text = self.tableData[indexPath.row];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
NSString *stringToMove = self.tableData[sourceIndexPath.row];
[self.tableData removeObjectAtIndex:sourceIndexPath.row];
[self.tableData insertObject:stringToMove atIndex:destinationIndexPath.row];
}
- (IBAction)startEditing:(id)sender {
self.editing = YES;
}
#end
Related
I'm creating a table view using custom table view cell using data from the NSSArray property of the controller class. when i ran the app only the first row of the table view display correctly.
#import "MainTableViewController.h"
#interface MainTableViewController ()
#property (strong, nonatomic) NSArray* items;
#end
#implementation MainTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
_items = #[#"Item1", #"Item2", #"Item3"];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _items.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
TableViewCell* tableViewCell = [tableView dequeueReusableCellWithIdentifier:#"TableViewCell"];
if (!tableViewCell) {
UINib* tableViewCellNib = [UINib nibWithNibName:#"TableViewCell" bundle:nil];
[tableView registerNib:tableViewCellNib forCellReuseIdentifier:#"TableViewCell"];
tableViewCell = [tableView dequeueReusableCellWithIdentifier:#"TableViewCell"];
}
return tableViewCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
CalculatorViewController* calculatorViewController = [[CalculatorViewController alloc] init];
[self presentViewController:calculatorViewController animated:YES completion:nil];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(TableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.label.text = [_items objectAtIndex:indexPath.row];
}
the first row renders Item1 but the rest only renders the first two character and dots for each row. ex It... for row 2 and the rest. the expected result is it displays everything in the _items array.
I am new to objective c in my tableview i populated the textfield dynamically in the uitableview cell its working fine but whenever the user types the values in the uitextfield the value get repeated to another cell textfield can anyone help me how to solve this error.This is my sample code and i also the added the sample screen shot below for your reference .
#import "TableViewController.h"
#import "TableViewCell.h"
#interface TableViewController ()<UITableViewDelegate>
#end
#implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.mytableview.dataSource=self;
self.mytableview.delegate=self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellidentifier=#"cell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier];
if (!cell) {
cell= [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier];
}
return cell;
}
#end
Actually, text appears on another cell because the cell is reused when you create cell by TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier]
Add this code to your TableViewCell.m
- (void)prepareForReuse {
self.textfield.text = #"";
}
TableViewController.m
#import "TableViewController.h"
#import "TableViewCell.h"
#define numberOfRow 20;
#interface TableViewController ()<UITableViewDelegate, UITextFieldDelegate>
#property (nonatomic, strong) NSMutableArray* texts;
#end
#implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
_texts = [[NSMutableArray alloc] init];
for (int i = 0; i < numberOfRow; i++) {
[_texts addObject:#""];
}
self.mytableview.dataSource=self;
self.mytableview.delegate=self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return numberOfRow;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellidentifier=#"cell";
TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier];
if (!cell) {
cell= [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentifier];
}
cell.textField.text = _texts[indexPath.row];
cell.textField.tag = indexPath.row;
cell.textField.delegate = self;
return cell;
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
_text[textField.tag] = textField.text;
}
#end
I have two viewControllers, ViewController and TableViewController. the ViewController has a button, when pressed the TableViewController will start. the TableViewContoller contains 5 rows.
what I am trying to do is, when I press only the even rows I should go back to ViewController. To solve this problem, we can use unwindSegue, but I do not know how to have the segue respond to the even rows in the table.
please let me know how to make a row in the table linked to the segue
below is the code for TableViewController
code:
#import "TableViewController.h"
#interface TableViewController ()
#property NSArray *tableData;
#end
#implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.tableData = [NSArray arrayWithObjects:#"Android",
#"iOS",
#"swift",
#"objective-c",
#"openCV",nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section {
return [self.tableData count];
}
-(UITableViewCell *) tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [self.tableData objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:#"images.jpg"];
return cell;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath {
NSLog(#"%li", indexPath.row);
NSLog(#"%#", [self.tableData objectAtIndex:indexPath.row]);
}
#end
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:
(NSIndexPath *)indexPath {
if(indexPath.row % 2 == 0) {
// Even row...
}
}
I am working on a project that is both iOS7 and iOS8. And I ran into this bug on iOS8, where the tableview will "jump" when selecting an item.
This bug is only there when estimated height is set and it differs from the row height.
Here are two gifs of the same code in iOS8 and iOS7. First iOS8:
And now in iOS7:
I have made a sample project showing the bug here: https://github.com/bjarkehs/TableViewiOS8Bug
I am not sure if I am simply missing something, but I am stuck with this issue and I haven't been able to find anything on this.
Here are my tableView methods:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 1) {
return 30;
}
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
cell.textLabel.text = [NSString stringWithFormat:#"%# %ld", #"Wat", indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 45.f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 50.f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.navigationController pushViewController:[UIViewController new] animated:YES];
}
Here is an example viewcontroller where the calculated height is not hardcoded, showing the same issue:
#import "ViewController.h"
#interface ViewController ()
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#end
#implementation ViewController {
NSMutableArray *_items;
CGFloat _estimatedHeight;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
_estimatedHeight = 70.f;
CGFloat randomOffset = 30.f;
_items = [NSMutableArray new];
for (NSInteger i = 0; i < 3; i++) {
NSMutableArray *tempItems = [NSMutableArray new];
for (NSInteger j = 0; j < 30; j++) {
NSInteger offset = arc4random_uniform(randomOffset);
[tempItems addObject:#(_estimatedHeight + (randomOffset/2) - offset)];
}
[_items addObject:tempItems];
}
// Do any additional setup after loading the view.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (section == 1) {
return 30;
}
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"cell"];
}
cell.textLabel.text = [NSString stringWithFormat:#"%# %ld", #"Wat", indexPath.row];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return _estimatedHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [[[_items objectAtIndex:indexPath.section] objectAtIndex:indexPath.row] floatValue];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.navigationController pushViewController:[UIViewController new] animated:YES];
}
#end
Your implementation for Estimated Height For At Index isn't something I've seen before. Have a look at this where I detailed using this pattern...
Estimated Height Example
How to make uitableview with multiple sections dynamically with dynamic array count ?
Check this long code, I think this will come handy
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#pragma mark TableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [self.words count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [[self.words objectAtIndex:section]count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
// UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:#"cell"];
// if (cell==nil) {
// cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
// }
//UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:#"cell"];
//if (cell==nil) {
UITableViewCell *cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
//}
cell.textLabel.text=[[self.words objectAtIndex:indexPath.section]objectAtIndex:indexPath.row];
cell.detailTextLabel.text=[NSString stringWithFormat:#"This is detailed subtitle for %#.",cell.textLabel.text];
//cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
NSString *imagePath=[[NSBundle mainBundle]pathForResource:#"images" ofType:#"jpeg"];
UIImage *image=[[UIImage alloc]initWithContentsOfFile:imagePath];
cell.imageView.image=image;
return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return [self.alphabets objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView;{
return self.alphabets;
}
#pragma mark -
#pragma mark Default
- (void)viewDidLoad
{
self.alphabets=[NSMutableArray new];
for (char i=97; i<123; i++) {
[self.alphabets addObject:[[NSString stringWithFormat:#"%c",i]capitalizedString]];
}
self.words=[NSMutableArray new];
for (NSString *character in _alphabets) {
NSMutableArray *twoD=[NSMutableArray new];
for (int i=1; i<(rand()%10)+1; i++) { //fill randamly any number 1 to 10.
[twoD addObject:[NSString stringWithFormat:#"%#%d",character,i]];
}
[self.words addObject:twoD];
}
// NSLog(#"->%#",self.words);
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
You probably should look at the UITableViewDataSource API documentation, specifically the -[UITableViewDataSource numberOfSectionsInTableView:] API.
You will also need to implement -[UITableViewDataSource tableView:numberOfRowsInSection:]. Both of these would need to be implemented in your class which implements the UITableViewDataSource protocol.
Please try to use this one....
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// ----- here you can return any value whatever you want. -----
return [array count];
}
You set totalSection and reload tableview
Before you set totalSection in [tableView reloadData];
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return totalSection;
}