3

很长的故事:

我的一个同事向我寻求一点帮助。我是一名 C# 开发人员,他是一名 iOS 开发人员,阅读彼此的代码有时会给我们一些很好的见解。

他正在编写一些函数,该函数需要返回一个基本类型的对象UITableViewCell并有一个整数作为输入。实际的返回类型是UITableViewCell. 他问我是否知道更好的解决方案,然后像这样的简单开关:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (row) {
        case 0: {
            NSString *CellIdentifier = @"personCell";

            PersonInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil) {
                cell = [[PersonInfoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            return cell;
            break;
        }
        case 1: {
            NSString *CellIdentifier = @"photoCell";

            PhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
            if (cell == nil) {
                cell = [[PhotoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
            }
            cell.selectionStyle = UITableViewCellSelectionStyleNone;

            return cell;
            break;
        }
        default:
            return nil; //Don't care about this atm. Not the problem.
            break;
    }
}

我的 C# 实现将如下所示:

public UITableViewCell TableViewCellForRowAtIndexPath(UITableView tableView, NSIndexPath indexPath)
{
    switch(indexPath.row)
    {
        case 0:
        return MakeCell<PersonInfoCell>();
        case 1:
            return MakeCell<PhotoCell>();
        default:
            return null; //Still doesn't matter
    }
}


public TCell MakeCell<TCell>() where TCell : UITableViewCell, new()
{
    TCell cell = new TCell();
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}

public class PersonInfoCell : UITableViewCell
{
    //Dont care about implementation yet....
    //TL;DR
}

public class PhotoCell : UITableViewCell
{
    //Dont care about implementation yet....
    //TL;DR
}

短篇故事:

有谁知道将我的 C# 通用代码转换为objective-c equilivant 的方法?


更新 1

我们基于 Nicholas Carey 的想法的错误实现。


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch (row) {
        case 0: {
            NSString *CellIdentifier = @"personCell";

            PersonInfoCell *cell = (PersonInfoCell *)[self makeCell:CellIdentifier];
            //Do PersonInfoCell specific stuff with cell
            return cell;
            break;
        }
        case 1: {
            NSString *CellIdentifier = @"photoCell";

            PhotoCell *cell = (PhotoCell *)[self makeCell:CellIdentifier];
            //Do PhotoCell specific stuff with cell
            return cell;
            break;
        }
        default:
            return nil; //Don't care about this atm. Not the problem.
            break;
    }
}

- (id *)makeCell:(NSString *)cellIdentifier 
{
    id cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[PhotoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; // <---- how does this method know it is a PhotoCell I want?
               //PhotoCell???
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    return cell;
}

4

3 回答 3

1

在 Objective-C 中,类是第一类构造,可以像任何其他对象一样传递和使用。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = nil;
    Class cellClass = nil;
    switch (row) {
        case 0:
            cellIdentifier = @"personCell";
            cellClass = [PersonInfoCell class];
            break;
        case 1:
            cellIdentifier = @"photoCell";
            cellClass = [PhotoCell class];
            break;
        default:
            return nil; //Don't care about this atm. Not the problem.
    }

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
        cell = [[cellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

如果你真的想排除工厂方法,你可以这样做:

- (UITableViewCell *)getOrCreateCellForTable:(UITableView *)tableView withIdentifier:(NSString *)cellIdentifier class:(Class)cellClass
{
  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
  if (cell == nil)
      cell = [[cellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

  cell.selectionStyle = UITableViewCellSelectionStyleNone;
  return cell;
}

如果您想摆脱成对的参数cellIdentifier和参数,一种选择是在您使用的每个单元类上cellClass创建一个defaultIdentifier类方法(方法,用 C# 的说法)。static这样,您可以只将类传递给工厂方法,工厂方法可以查询类以获取正确的标识符。

于 2013-05-22T18:36:35.777 回答
1

基于 David Mitchell 的代码,我们(Synercoder 的同事)想出了这个,效果很好!

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = nil;
    Class cellClass = nil;
    switch (indexPath.row) {
        case 0: {
            cellIdentifier = @"personCell";
            cellClass = [PersonInfoCell class];
            PersonInfoCell *personInfoCell = (PersonInfoCell *)[self getOrCreateCellForTable:tableView withIdentifier:cellIdentifier class:cellClass];
            personInfoCell.delegate = self;
            return personInfoCell;
        }
        case 1: {
            cellIdentifier = @"photoCell";
            cellClass = [LastMeasureMent class];
            LastMeasureMent *measurementCell = (LastMeasureMent *)[self getOrCreateCellForTable:tableView withIdentifier:cellIdentifier class:cellClass];
            measurementCell.selectionStyle = UITableViewCellSelectionStyleBlue;
            return measurementCell;
        }
        default: {
            cellIdentifier = @"photoCell";
            cellClass = [LastMeasureMent class];
            LastMeasureMent *measurementCell = (LastMeasureMent *)[self getOrCreateCellForTable:tableView withIdentifier:cellIdentifier class:cellClass];
            measurementCell.selectionStyle = UITableViewCellSelectionStyleBlue;
            return measurementCell;
        }
    }


}

- (UITableViewCell *)getOrCreateCellForTable:(UITableView *)tableView withIdentifier:(NSString *)cellIdentifier class:(Class)cellClass
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil)
        cell = [[cellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

谢谢大卫!

于 2013-05-22T18:53:44.423 回答
0

请参阅这个问题:Objective-C 中是否存在强类型集合?

Objective-C 是后期绑定和动态类型的,所以你不需要泛型。您可以向对象发送任何消息。它用它做什么取决于对象。具有静态类型的早期绑定语言,如 C#、Java、C++ 需要泛型。没有它们,问题就不会[轻易地]知道它可以或不能对包含的对象做什么。

于 2013-05-22T17:27:41.520 回答