indexPath在所有行中返回0

时间:2014-08-06 12:15:01

标签: ios objective-c uitableview

我创建了一个只有1个部分的tableView,并且在所有indexPath.Row中一直返回0。我究竟做错了什么?这是我记录indexPath

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 个答案:

答案 0 :(得分:12)

问题可能是这个

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

而不是

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

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

答案 1 :(得分:1)

您可以交换numberOfSectionsInTableViewnumberOfSectionsInTableView

的返回值

如果这是正确的,下面的日志会给你一个想法。

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

这将按照部分记录行号。

示例:如果您有一个包含2个部分和6行(每个部分中有3行)的TableView,则会记录为

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

答案 2 :(得分:0)

可能是因为你没有创建UITableViewCell的对象而没有分配。 请查看下面的代码。

- (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
}

还要检查numberOfSectionsInTableView和numberOfRowsInSection UITableView Delegate方法。

问候。

相关问题