2014-09-24 3 views
0

Есть ли класс или каким-то образом, как ArrayAdapter в AndroidОбщий класс для UITableView

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

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

мы просто установить адаптер к ListView (TableView в IOS). Я собираюсь создать этот тип сценария.

Для этого я создал класс, в котором записаны все делегаты 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 находятся в рабочем состоянии и работают по мере необходимости.

+0

Вам понадобится ссылка на объект UITableView, чтобы установить его делегат и источник данных в ваш класс api_tableview. У вас есть такая ссылка? Также стоит отметить, что классы Obj-c начинаются с заглавной буквы, как и на Java. :) – ABeanSits

+0

Прочтите мое Редактировать, пожалуйста. и предложите мне способ отправить UITableViewCell и его данные в api_tableview – Nepster

ответ

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; 
} 
+0

, ваш ответ поможет вам. но теперь я хочу передать UITableViewCell и соответствующим образом установить его значение. Предложите мне способ – Nepster

Смежные вопросы