Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UITableView中的自定义单元格未正确显示-仅填充最后一个单元格_Ios_Objective C_Uitableview - Fatal编程技术网

Ios UITableView中的自定义单元格未正确显示-仅填充最后一个单元格

Ios UITableView中的自定义单元格未正确显示-仅填充最后一个单元格,ios,objective-c,uitableview,Ios,Objective C,Uitableview,如果这是一个初学者的问题,请道歉。我正在尝试用节和自定义单元格格式填充UITableView 我在ViewControl.xib中创建了一个customCell,它位于主视图上,如下所示: 我有一个字典,可以使用另一个类中的方法加载值,具体取决于它所在的行。如果在第1行,则加载第1项的详细信息等 这是我正在使用的代码: -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPat

如果这是一个初学者的问题,请道歉。我正在尝试用节和自定义单元格格式填充UITableView

我在ViewControl.xib中创建了一个customCell,它位于主视图上,如下所示:

我有一个字典,可以使用另一个类中的方法加载值,具体取决于它所在的行。如果在第1行,则加载第1项的详细信息等

这是我正在使用的代码:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"customCell"];

    // assigns current row's labels
    NSArray * customCellText = [Model cellText:indexPath.row];
    dinnerItem.text = customCellText[0];
    dinnerDescription.text = customCellText[1];
    dinnerTime.text = customCellText[2];

    cell = customCell;
    return cell;
}
这就是目前正在生成的内容:

我的问题是:

  • 它不是填充所有行,只是最后一行
  • 我似乎只能单击已填充的行,甚至 它保持选中状态,而不是“单击”
  • 如果我很快地把它向上或向下拖,它就会崩溃
我想这与它重新绘制/填充细胞的方式有关

提前谢谢! 西奈二号

编辑,添加更多代码进行澄清:

ViewController.m

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return [Model countKeys];
    }

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [Model rowsInSection:section];
    }

    -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
// slightly crap code, this is initiated in viewDidLoad and is an array created by a method in Model.m. Only looks for keys and returns an array.
        return headerKeys[section];
    }
+(NSArray *)headerKeys
{
    NSArray *headerKeys = [[NSArray alloc] init];
    headerKeys = [timerDictionary allKeys];
    NSLog(@"All keys: %@", headerKeys);
    return headerKeys;
}

+(NSArray *)customCellText
{
    NSArray *customCellText = [[NSArray alloc]initWithObjects: @"dinnerItemText", @"dinnerDescriptionText", @"01:00", nil];
    return customCellText;
}

+(NSInteger)rowsInSection:(NSInteger)sectionNumber
{
    NSArray *keyContent = [[NSArray alloc] init];
    keyContent = [timerDictionary objectForKey:dictionaryKeys[sectionNumber]];
    NSLog(@"current section[%i]: %i", sectionNumber, [keyContent count]);
    return [keyContent count];
}

+(NSArray *)cellText:(NSInteger)rowNumber
{
    // display all dictionary keys, dictionaryKeys[x] will give back the specific category
    dictionaryKeys = [timerDictionary allKeys];

    // displays contents of first key in dictionary
    NSArray *keyContent = [[NSArray alloc] init];
    keyContent = [timerDictionary objectForKey:dictionaryKeys[0]];

    // creates an array with all items within the selected key
    NSArray *keyItems = [[NSArray alloc] initWithArray:keyContent[rowNumber]];
    return keyItems;
}
Model.m

    -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return [Model countKeys];
    }

    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [Model rowsInSection:section];
    }

    -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
// slightly crap code, this is initiated in viewDidLoad and is an array created by a method in Model.m. Only looks for keys and returns an array.
        return headerKeys[section];
    }
+(NSArray *)headerKeys
{
    NSArray *headerKeys = [[NSArray alloc] init];
    headerKeys = [timerDictionary allKeys];
    NSLog(@"All keys: %@", headerKeys);
    return headerKeys;
}

+(NSArray *)customCellText
{
    NSArray *customCellText = [[NSArray alloc]initWithObjects: @"dinnerItemText", @"dinnerDescriptionText", @"01:00", nil];
    return customCellText;
}

+(NSInteger)rowsInSection:(NSInteger)sectionNumber
{
    NSArray *keyContent = [[NSArray alloc] init];
    keyContent = [timerDictionary objectForKey:dictionaryKeys[sectionNumber]];
    NSLog(@"current section[%i]: %i", sectionNumber, [keyContent count]);
    return [keyContent count];
}

+(NSArray *)cellText:(NSInteger)rowNumber
{
    // display all dictionary keys, dictionaryKeys[x] will give back the specific category
    dictionaryKeys = [timerDictionary allKeys];

    // displays contents of first key in dictionary
    NSArray *keyContent = [[NSArray alloc] init];
    keyContent = [timerDictionary objectForKey:dictionaryKeys[0]];

    // creates an array with all items within the selected key
    NSArray *keyItems = [[NSArray alloc] initWithArray:keyContent[rowNumber]];
    return keyItems;
}

首先,将您的方法更改为此!您需要检查单元格是否为
nil

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

        // assigns current row's labels
        NSArray * customCellText = [Model cellText:indexPath.row];
        dinnerItem.text = customCellText[0];
        dinnerDescription.text = customCellText[1];
        dinnerTime.text = customCellText[2];

        cell = customCell;
    }


    return cell;
}

我可以说:

如果您只这样做:
UITableViewCell*单元格=(UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:@“customCell]”;

在iOS 5之前,这并不能保证你能拿回手机。在iOS5之前,您必须执行以下操作:

NSString*cellIdentifier=[NSString stringWithFormat:@“I%d-%d”,indexath.section,indexath.row]

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"customCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    // assigns current row's labels
    NSArray * customCellText = [Model cellText:indexPath.row];
    dinnerItem.text = customCellText[0];
    dinnerDescription.text = customCellText[1];
    dinnerTime.text = customCellText[2];

    cell = customCell;
    return cell;
}
如果您使用的是iOS5/6,则不再需要这些行:

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        }
但是您必须在tableView设置中使用以下方法:

registerClass:forCellReuseIdentifier:  (available in iOS6 and later)
registerNib:forCellReuseIdentifier: (available in iOS5 and later)
只是希望解决您的问题:

不要忘记第[…]行(cell==nil)[…]

是否可以发布UITableViewController实现的其余部分?是否可以发布其他数据源方法?(即,
numberOfRowsInSection
)您所说的
cell=customCell是什么意思?什么是customCell?你能告诉我们你在哪里设置customCell的代码吗?嗨@danielrsmith-我已经在我的问题下面添加了额外的代码。谢谢@MikeD,我添加了其他方法。ThanksI已将其更改为检查单元格是否为零,但不幸的是,它无法解决问题(我在提交问题之前对此进行了测试,但希望使其尽可能简单,以便人们提供帮助)。我现在知道它需要在那里,所以感谢您让我知道。不要打包
//分配当前行的标签NSArray*customCellText=[Model cellText:indexPath.row];dinnerItem.text=customCellText[0];dinnerDescription.text=customCellText[1];dinnerTime.text=customCellText[2];单元格=自定义单元格在if上下文中!!!如果重复使用,您的计算单元将不会更新为相应的infosI can vote up,但我最终使用customCell创建了一个新的xib,并使用了类似的实现。以前它是与ViewController共享的,上面的代码不太适合它。您正确地指出cell=customCell不正确,因为它会覆盖它。最后我做了
cell.dinnerItem.text=customCellText[0];cell.dinnerDescription.text=customCellText[1];cell.dinnerTime.text=customCellText[2];返回单元。我在这里遇到了一个新问题,但我相信我能自己解决。谢谢