UITableView的通用类

时间:2014-09-24 10:55:25

标签: android ios uitableview delegates

是否有像Android中的ArrayAdapter类或某种方式

ListView listView = (ListView) findViewById(R.id.list);

ArrayAdapter<MyClass> adapter = new ArrayAdapter<MyClass>(this, R.layout.row,
    to, myList.);
listView.setAdapter(adapter);

我们只是将适配器设置为listView(IOS中的TableView)。我将创建这种类型的场景。

为此我创建了一个类,其中写了TableView的所有委托。如何将UITableViewDelegate,UITableViewDatasource连接到该类。

api_tableview.h

#import <Foundation/Foundation.h>

@protocol didSelectedProtocol<NSObject> // creating protocol

-(void)didSelectedProtocolMethod :(NSString *)str;

@end

@interface api_tableview : NSObject<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,retain) NSMutableArray *tabledata;

@property(nonatomic,retain)UITableView *mytable;

@property(nonatomic,retain)UITableViewCell *cello;

-(void)initTableviewWithData:(UITableView *)table dataToShow:(NSMutableArray *)arr;

-(void)initTableviewAndTableCellWithData:(UITableView *)table cellObject:(UITableViewCell *)mycell dataToShow:(NSMutableArray *)arr;

@property(strong,nonatomic) id<didSelectedProtocol> didSelectedDelegate;//didSelectedProtocol

@end

api_tableview.m

#import "api_tableview.h"

@implementation api_tableview
{}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier"];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    NSDictionary *item = (NSDictionary *)[self.tabledata objectAtIndex:indexPath.row];

    cell.textLabel.text =[_tabledata objectAtIndex:indexPath.row];

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // here we get the cell from the selected row.
    UITableViewCell *selectedCell=[tableView cellForRowAtIndexPath:indexPath];

    NSString *texttoshow = selectedCell.textLabel.text;
    [self.didSelectedDelegate didSelectedProtocolMethod:texttoshow];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(void)initTableviewAndTableCellWithData:(UITableView *)table cellObject:(UITableViewCell *)mycell dataToShow:(NSMutableArray *)arr
{
    _mytable =table;

    _mytable.delegate =self;

    _mytable.dataSource =self;

    _tabledata =arr;

    _cello =mycell;
}
-(void)initTableviewWithData:(UITableView *)table dataToShow:(NSMutableArray *)arr
{
    _mytable =table;

    _mytable.delegate =self;

    _mytable.dataSource =self;

    _tabledata =arr;
}

@end

我创建了一个tableView属性,但如果我在这里得到它,我不知道如何连接它UITableViewDatasource,UITableViewDelegate

建议我。

提前致谢...

修改

我成功地创造了我想要的一件事。我的类只适用于默认的tableViewCell。我怎么能设法发送我的tableViewCell和数组或字典,它将根据我设计的UITableCellView设置它。

注意

现在我用新的代码替换了旧代码。现在.h和.m正处于运行状态并按我的需要工作。

1 个答案:

答案 0 :(得分:0)

你应该设置UITableView UITableViewDataSource,UITableViewDelegate。喜欢

_tableView.dataSource = self;
_tableView.delegate = self;

当你有数据时,你只应该调用UITableView的函数“reloadData”,委托函数就可以了。

 [_tableView reloadData]  (MayBe you need this)


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

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

      Class *data = [_tabledata objectAtIndex:indexPath.row];
        // create Cell with data;
        return cell;
}