IndexPath returns 0 on all rows

I created tableView

with only 1 section and it returns 0 in all indexPath.Row

. What am I doing wrong? here i registerindexPath

NSLog (@ "% @", indexPath);

<NSIndexPath: 0xd2897d0> {length = 2, path = 0 - 0}
<NSIndexPath: 0xd290af0> {length = 2, path = 1 - 0}
<NSIndexPath: 0xd2a5cd0> {length = 2, path = 2 - 0}
<NSIndexPath: 0xd2a8340> {length = 2, path = 3 - 0}

      

cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {



    UITableViewCell *cell = nil;


    if (indexPath.row < 3) {
        TextFieldTableViewCell *accountCell = [tableView dequeueReusableCellWithIdentifier: textFieldCellIdentifier forIndexPath: indexPath];
        accountCell.cellTextField.delegate = self;

            accountCell.cellLabel.text = [accountArray objectAtIndex: indexPath.row];
        if (indexPath.row == 0) {


        accountCell.cellTextField.keyboardType = UIKeyboardTypeDefault;
        } else if (indexPath.row == 1) {


            accountCell.cellTextField.keyboardType = UIKeyboardTypeNumberPad;
        } else if (indexPath.row == 2) {


            accountCell.cellTextField.keyboardType = UIKeyboardTypeNumberPad;
        }

        cell = accountCell;


    } else if (indexPath.row == 3) {

        ACEExpandableTextCell *textViewCell = [tableView expandableTextCellWithId:@"cellId"];
        textViewCell.text = [self.cellData objectAtIndex:indexPath.section];
        textViewCell.textView.placeholder = @"Placeholder";
        cell = textViewCell;

    }


    return cell;
}

      

+3


source to share


3 answers


The problem might be that

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [arry count];
}   
 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    return 1;
    }

      



instead

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    return [arry count];
    }

      

+11


source


You can change the return value numberOfSectionsInTableView

andnumberOfSectionsInTableView

If that's correct, the log below will give you an idea.

 NSLog(@"Section Number %d", indexPath.section);
 NSLog(@"Row Number %d", indexPath.row);

      



This will register the line numbers according to the section.

Example. If you have a TableView with 2 sections and 6 rows (3 rows in each section) this will log as

Section Number 0
Row Number 0
Section Number 0
Row Number 1
Section Number 0
Row Number 2

Section Number 1
Row Number 0
Section Number 1
Row Number 1
Section Number 1
Row Number 2

      

+1


source


It could be because you did not create the UITableViewCell object and were not allocated. please see below code once.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
    }

    //Here add your stuffs
}

      

And also check the NumberOfSectionsInTableView and numberOfRowsInSection methods of the UITableView Delegate.

Sincerely.

0


source







All Articles