When I open the table view it calls the function bellow but returns nill for the Cell. But when I scroll down it calls the function, and return the proper values. Thanks in Advance

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
        static NSString *CellIdentifier = @"Formal";
        UITableViewCell *cellt = [self.gridtable dequeueReusableCellWithIdentifier:CellIdentifier];

        if(labelArray.count==0)
            indexPath=0;
        else
            cellt.textLabel.text= [labelArray objectAtIndex:indexPath.row];//CellIdentifier;//[labelArray objectAtIndex:2];


        if (cellt == nil) {
            cellt = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        }
return cellt;
}
有帮助吗?

解决方案

for one you should do the if (cellt == nil) before the setting of your textLabel. Because right now you are setting the text, then initing the cell which then erases the text because your cell didn't exist in the first place.

This is what a proper - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"Formal";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    cell.textLabel.text = [labelArray objectAtIndex:indexPath.row];

    return cell;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top