2013-03-13 60 views
4

我想在同一个xib中使用具有UITableView的自定义UITableviewCell而不创建UITableViewCell类?如何在同一个xib中使用自定义的UITableViewCell和UITableView

正如你可以看到波纹管我设置标识符的UITableViewCell和使用它像这样:

UITableView and UITableViewCell in same xib

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    { 
    static NSString *CellIdentifier = @"CustomIdentifier"; 

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

} 

但不工作?

回答

0

是的,你可以做下面的代码

在viewDidLoad中或viewWillAppear中

label1Array=[NSMutableArray arrayWithObjects:@"A",@"B",nil]; 
label2Array=[NSMutableArray arrayWithObjects:@"C",@"D",nil]; 
label3Array=[NSMutableArray arrayWithObjects:@"E",@"F",nil]; 

的UITableViewDelegate

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

static NSString *CellIdentifier = @"aCell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

if (cell == nil) { 
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
UILabel *label1=[[UILabel alloc]init]; 
label1.frame=CGRectMake(0,0,40,40); 
label1.text=[label1Array objectAtIndex:indexPath.row]; 
................ 
[cell.contentView addSubView: label1]; 

UILabel *label2=[[UILabel alloc]init]; 
label2.frame=CGRectMake(50,0,40,40); 
label2.text=[label2Array objectAtIndex:indexPath.row]; 
[cell.contentView addSubView: label2]; 

UILabel *label3=[[UILabel alloc]init]; 
label3.frame=CGRectMake(100,0,40,40); 
label3.text=[label3Array objectAtIndex:indexPath.row]; 
[cell.contentView addSubView: label3]; 
} 

希望这有助于!

+0

感谢您的重播,但我想使用一个可视化的UiTableCell,并且无需在xib中为它创建类,这很容易在故事板中执行此操作,但是我不知道如何在xib文件中执行此操作 – 2013-03-13 07:13:33

+0

@ArashZeinoddini实际上此代码是只为xib ..可以简要说明你的问题,我不能得到你的问题。 – Dany 2013-03-13 07:14:31

+0

你知道,我想创建一个自定义的UITableViewCell,好吗?和正常的流程来做到这一点是使它的类,如本教程UITableViewCell:http://www.altinkonline.nl/tutorials/xcode/uitableview/ uitableviewcell /,好吗?,但我真的不想做自定义的UITableViewCell类(.h/.m),只是想创建一个UITableViewCell在我创建UITableView,好吧,当然我设置了一个标识符使用此创建了UITableViewCell。 – 2013-03-13 07:23:19

3

一个UITableViewCell * customCell属性添加到您的视图控制器(例如,您的文件ShowUsers.h)...

@property (nonatomic, strong) IBOutlet UITableViewCell *customCell; 

,并将其连接到您的厦门国际银行的自定义单元格。然后使用cellForRow中的以下代码(例如,您的文件ShowUsers.m):

if (cell == nil) { 
    [[NSBundle mainBundle] loadNibNamed:@"NibName" owner:self options:nil]; 
    cell = self.customCell; 
    self.customCell = nil; 
} 
+0

谢谢,但UITableViewCell和UItableView在一个xib中 – 2013-03-13 07:26:09

+0

您可以将tableviewcell移动到单独的xib。 – jamihash 2013-03-13 07:28:14

+0

这似乎确实完美地工作,2013年11月。 – Fattie 2013-11-01 09:33:05

0

请参阅...您可以这样做。这里你使用XIB在一个TableView中添加两个UITableViewCell。

在Class.h文件中: -

声明一个用于单元数量的可变数组。

 { 
     NSMutableArray * sectionRows; 
     } 

    @property (nonatomic,retain) IBOutlet UITableViewCell *addTvc1; 
    @property (nonatomic,retain) IBOutlet UITableViewCell *addTvc2; 

在Class.m: -

@synthesize addTvc1, addTvc2; 

    - (void)viewDidLoad 
     { 
     [super viewDidLoad]; 

     sectionRows = [[NSMutableArray alloc] init]; 

     [sectionRows addObject:[[NSMutableArray alloc] 
         initWithObjects:addTvc1, nil]]; 

     [sectionRows addObject:[[NSMutableArray alloc] 
         initWithObjects:addTvc2, nil]]; 

    } 

在的UITableViewDelegate方法 -

添加这 -

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
    { 
     return [sectionRows count]; 
    } 

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

    - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath: 
                 (NSIndexPath *)indexPath 
    { 
     return [[sectionRows objectAtIndex:indexPath.section] 
           objectAtIndex:indexPath.row]; 
    } 

随着代码,在类的XIB下降和拖曳两根的UITableViewCell这应该在视图之外,并添加文件所有者的单元格。 它会工作完美。这里不需要创建一个单独的自定义UITableViewCell类。

+0

如果您有任何与代码有关的疑问,请问......? – 2013-03-13 08:11:56

+0

最新花花公子?它在工作.... – 2013-03-13 10:13:18

相关问题