문제

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