开发者

Possible to tag a section in UITableView?

I have a uitableview with cel开发者_如何学JAVAls whose behavior depends on what:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
  if (indexPath.section == 2) {
    do x;
   }else{
    do y;
  }   
}

It kind of works when you are dealing with a single table, but when you try to add sections, or subclass things, this type of "magic numbers" break very easily.

My question is, is it possible to "tag" the section? so instead of doing section==1, we will do:

indexPath.section.tag=="user_stats" {load x}
indexPath.section.tag=="answers" {show answers}
indexPath.section.tag=="page" {show pagination}


Yeah, just set up an enum somewhere.

enum {  kSectionUserStats = 0,
        kSectionAnswers,
        kSectionPage };

Then:

if(indexPath.section == kSectionPage)
{
    // do x
} else if(indexPath.section == kSectionAnswers)
{
    // do y
}
// etc.

This also lets you reorder your sections really easily just by changing their ordering in the enum.


If you are displaying section headers with titles, then the titles themselves can become your tags.

if([self titleForHeaderInSection:indexPath.section] == @"MySection1")
{
     //do something
}
else
{
     //do something else
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜