0

我正在尝试 UITableView in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath使用在 viewDidLoad 方法中初始化的数组填充方法。

self.questionsArray = [NSArray arrayWithObjects:@"Q1",@"Q2",@"Q3",@"Q4",@"Q5",@"Q6",@"Q7",@"Q8",@"Q9",@"Q7",@"Q8",@"Q9",@"Q10",@"Q11"];

我了解如何实现各个部分并重用带有标识符的单元格。正如我见过的大多数例子一样,我尝试过 question.text = [NSString stringWithFormat:@"%d. %@", indexPath.row ,[self.questionsArray objectAtIndex:indexPath.row]];

但我越来越喜欢 0. Q1 1. Q2 2. Q3 3. Q4 4. Q5 5. Q6 0. Q1 2. Q2 ....

请帮助将所有问题(NSString)加载到我的 TableView。我搞砸了 indexPath.row。它非常难以理解。有关有效处理 indexPath 的最佳实践的任何提示。

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0:
            return [self.questionsArray count];
        case 1:
            return 1;
        default:
            return 0;
    }
}    
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{        
    if (indexPath.section == 0) {           
        static NSString *QuestionCellIdentifier = @"QuestionCellIdentifier";

        UITableViewCell *cell =  [tableView dequeueReusableCellWithIdentifier:QuestionCellIdentifier];

        if (cell == nil) {
                [[NSBundle mainBundle] loadNibNamed:@"QuestionCell" owner:self options:nil];
            }
            cell = questionCell; // IBoutlet    
            self.questionCell = nil;              
        }

           question.text = [NSString stringWithFormat:@"%d.  %@", indexPath.row ,[self.questionsArray objectAtIndex:indexPath.row]];

        return cell;    
    }       
    else if (indexPath.section == 1){

        static NSString *CustomCellIdentifierMore = @"CustomCellIdentifierMore";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CustomCellIdentifierMore];

        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CustomCellIdentifierMore] autorelease];
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
            switch (indexPath.row) {

                case 0:
                    cell.textLabel.text=@"Select";
                    break;                

                default:
                    break;
            }
        }   

        return cell;
    } 
    return nil;
}  
4

3 回答 3

1

cellForRowAtIndexPath 不能保证按顺序加载单元格,并且会被调用来更新某些单元格。您还需要提供 numberOfRowsInSection 方法,例如:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.tableData count];
}

您的单元格是否正确显示,还是只是 nslog 产生了令人困惑的输出?

于 2012-04-24T01:44:27.327 回答
0

在 viewDidLoad 中创建 questionsArray 后,尝试在 tableView 上调用 reloadData,如下所示:

self.questionsArray = [NSArray arrayWithObjects:@"Q1",@"Q2",@"Q3",@"Q4",@"Q5",@"Q6",@"Q7",@"Q8",@"Q9",@"Q7",@"Q8",@"Q9",@"Q10",@"Q11"];
//or whatever your property for the tableview is called.
[self.tableView reloadData];
于 2012-04-24T01:54:35.280 回答
0

您的 cellForRowAtIndexPath 实现有点古怪。首先, indexPath.section == 0 条件的大括号不匹配。其次,我看不到您在哪里为要返回的单元格设置文本。无论您分配给 question.text 什么,都应该分配给 cell.textLabel.text。第三,对于 indexPath.section == 1 条件,您仅在单元​​格为 nil 时设置文本。表格视图在单元管理方面非常聪明。当一个单元格由于滚动而超出视野时,它将重新使用该单元格,而不是完全创建一个新单元格。所以,每当你对一个单元进行出队时,它可能并不总是为零。因此,您应该在 if (cell == nil) 循环之外设置单元格文本。

于 2013-04-15T03:50:24.717 回答