2 أنواع مختلفة من UITableViewCells مخصصة في UITableView

StackOverflow https://stackoverflow.com/questions/1405688

  •  05-07-2019
  •  | 
  •  

سؤال

وفي بلدي UITableView أريد أن تعيين أول خبر من تغذيات مخصص tableViewCell (نوع A يتيح القول) وللأخبار أخرى الثانية والثالثة وما إلى ذلك .. آخر مخصص tableViewCell (trype B) المشكلة هي أن يتم استخدامها مخصص tableViewCell (trype A) التي تم إنشاؤها لأول خبر، ولكن الغريب عدد الصفوف بين أول استخدام لcustomViewCell (نوع A) وظهور الثاني من نفس النوع من customViewCell لا يساوي ..

وبلدي cellForRowAtIndexPath يبدو مثل هذا.

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    int feedIndex = [indexPath indexAtPosition:[indexPath length] - 1];
    Feed *item = [[[[self selectedButton] category] feedsList] objectAtIndex:feedIndex + 1];
    static NSString *CellIdentifier = @"Cell";

    if(feedIndex == 0){
        MainArticleTableViewCell *cell = (MainArticleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil)
        {
            cell = [[[MainArticleTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
            [[[cell subviews] objectAtIndex:0] setTag:111];
        }

        cell.feed = item;

        return cell;

    }
    else{
        NewsTableViewCell *cell = (NewsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil)
        {
            cell = [[[NewsTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier orientation:currentOrientation] autorelease];
            [[[cell subviews] objectAtIndex:0] setTag:111];
        }

        cell.feed = item;

        return cell;

    }
    return nil;
}    

وعلى هذين النوعين من الخلايا لديها ارتفاعات مختلفة التي تم تعيينها بشكل صحيح. يمكن أن تشير لي أحدهم في الاتجاه الصحيح بشأن كيفية جعل نوع خلية مخصصة لتظهر فقط لأول خبر (لا يتم إعادة استخدامها)؟ شكرا لك

هل كانت مفيدة؟

المحلول

ويجب إنشاء معرف خلية مختلفة لأنماط اثنين من خلية:

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

int feedIndex = [indexPath indexAtPosition:[indexPath length] - 1];
Feed *item = [[[[self selectedButton] category] feedsList] objectAtIndex:feedIndex + 1];

static NSString *CellIdentifier1 = @"Cell1";
static NSString *CellIdentifier2 = @"Cell2";

if(feedIndex == 0) {

   MainArticleTableViewCell *cell = (MainArticleTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];

   if (cell == nil) {
       cell = [[[MainArticleTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier1] autorelease];
       [[[cell subviews] objectAtIndex:0] setTag:111];
   }

   cell.feed = item;

   return cell;
}
else {
   NewsTableViewCell *cell = (NewsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier2];

   if (cell == nil) {
       cell = [[[NewsTableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier2 orientation:currentOrientation] autorelease];
       [[[cell subviews] objectAtIndex:0] setTag:111];
   }

   cell.feed = item;

   return cell;
}

return nil;
}

نصائح أخرى

وأنا لا أفهم تماما سؤالك، ولكن لاحظت أمرين غريبة. إذا كنت تستخدم نوعين من أنواع الخلايا تحتاج إلى استخدام اثنين من معرفات خلية متميزة عند استدعاء "dequeueReusableCellWithIdentifier. أنت الآن تستخدم نفس المعرف على حد سواء، وهو غير صحيح. جرب شيئا مثل:

static NSString *MainArticleIdentifier = @"MainArticle";
static NSString *NewsIdentifier = @"News";

وأيضا، وأنا لم أر قط شيئا مثل:

int feedIndex = [indexPath indexAtPosition:[indexPath length] - 1];

لماذا لا تستخدم فقط:

int feedIndex = indexPath.row;

وفي cellForRowAtIndexPath

if ("Condition for cell 1") {
        cellV = ("customCell" *)[tableView dequeueReusableCellWithIdentifier:@"your ID cell in .xib"];

        if (cellV == nil) {
            [[NSBundle mainBundle] loadNibNamed:@"YOUR-CELL-FILENAME" owner:self options:nil];
            cellV = "OUTLET-CEll-IN-VC";
        }
    } else {
        cellV = ("customCell" *)[tableView dequeueReusableCellWithIdentifier:@"your ID cell2 in .xib"];

        if (cellV == nil) {
            [[NSBundle mainBundle] loadNibNamed:@"YOUR-CELL2-FILENAME" owner:self options:nil];
            cellV = "OUTLET-CEll-IN-VC";
        }
    }

[self configureCell:cellV indexpath:indexPath withClipVo:clip];

return cellV;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top