rayesquire
rayesquire

Reputation: 19

In UITableView,cellForRowAtIndexPath ignore the first few rows

I've used sqlite in my project and I get ten elements in _mChats (array) successfully. The numberOfRowsInSection method returns 10 but cellForRowAtIndexPath ignores the first few rows and in simulator other elements all display on the same cell,why? And if I use sections instead of rows to display data,everything becomes normal.

This is my code:

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

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        VChat *cell = [VChat vChatWithTableView:tableView];
        cell.mChat = _mChats[indexPath.section];
        return cell;
    }

Upvotes: 0

Views: 583

Answers (4)

lynulzy
lynulzy

Reputation: 571

The three delegate method must be implement you have wrote have something wrong. As @Abhinav said the method return cell have some thing wrong.

As the code you showed will create a new cell when a new cell present in the screen. I think the first few rows just because the cell has't be reused.

There is another thing I want to say, if you are using storyboard to build the tableview this method should seems like:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    // The identifier in storyboard you setted.
    static NSString *cellName = @"theCell";
    Cell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (dataSource_[indexPath.row]) {
        // This method will bind data to view
        [cell fillDataWithDict:dataSource_[indexPath.row]];
    }
    return cell;
}

Upvotes: 0

user3182143
user3182143

Reputation: 9599

- (UITableViewCell *)tableView:(UITableView *)iTableView cellForRowAtIndexPath:(NSIndexPath *)iIndexPath 
{
   VChat *cell = (VChat *)[tableViewdequeueReusableCellWithIdentifier:@"CellIdentifier"];
   NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"VChat" owner:self options:nil];
   if (cell==nil)
   {
    cell = nib[0];
   }
   cell.mChat = _mChats[indexPath.row];
   return cell;
}

Upvotes: 0

Abhinav
Abhinav

Reputation: 38162

This is how your cellForRowAtIndexPath should look like:

- (UITableViewCell *)tableView:(UITableView *)iTableView cellForRowAtIndexPath:(NSIndexPath *)iIndexPath {
    NSString *cellIdentifier = @"myCell";

    UITableViewCell *cell = (UITableViewCell *)[iTableView dequeueReusableCellWithIdentifier:cellIdentifier];

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

    cell.mChat = _mChats[indexPath.row];

    return cell;
}

Upvotes: 0

Saheb Roy
Saheb Roy

Reputation: 5967

I think its because of this cell.mChat = _mChats[indexPath.section];

Try this instead cell.mChat = _mChats[indexPath.row];

This is because Indexpath is a property that has two values inside it, Eg. IndexPath [section,row] meaning indexpath of something is section number 1, row number 5. Hope this helps you understand.

Upvotes: 1

Related Questions