2013-02-07 28 views
14

Tôi có một câu hỏi đơn giản liên quan đến chế độ xem bảng với 3 loại tế bào nguyên mẫu khác nhau. Hai cái đầu tiên xảy ra chỉ một lần trong khi lần thứ ba xảy ra 4 lần. Bây giờ những gì tôi đang nhầm lẫn về là làm thế nào để xác định trong cellforRowatindexpath của tôi mà nguyên mẫu tế bào để sử dụng cho hàng nào. Vì vậy, tôi muốn một cái gì đó như cho hàng 0, sử dụng nguyên mẫu 1, cho hàng 1, sử dụng nguyên mẫu 2, cho hàng 3,4,5 và 6 sử dụng nguyên mẫu 3. Cách tốt nhất để làm điều này là gì? Tôi có cung cấp cho mỗi nguyên mẫu một định danh và sau đó sử dụng dequeueReusableCellWithIdentifier: CellIdentifier? Bạn có thể cung cấp một số mã mẫu không?TableView với nhiều ô mẫu thử

CHỈNH SỬA:

Vẫn không hoạt động. Đây là mã tôi có vào lúc này. (Tôi chỉ có một trường hợp cho Statment tắc vì tôi chỉ muốn thử nghiệm và thấy nếu ô đang được tạo trong hàng đầu tiên hay không, nhưng hiện tại xem bảng là trống)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    switch(indexPath.row) 
{   
case 0: {static NSString *CellIdentifier = @"ACell"; 
        UITableViewCell *cell = [tableView 
              dequeueReusableCellWithIdentifier:@"ACell"]; 
    if(cell==nil) { 
    cell=[[UITableViewCell alloc] 
      initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"ACell"]; 

         } 
    return cell; 
    break; 
    } 
    } 
} 

Acell là định danh của tôi cho một nguyên mẫu tế bào mà tôi đã tạo ra. Tôi

+0

Tôi không undertand câu hỏi của bạn. –

+2

"Tôi có cung cấp cho mỗi nguyên mẫu một định danh và sau đó sử dụng dequeueReusableCellWithIdentifier: CellIdentifier?" Có, bạn làm. Bạn đã khá nhiều câu trả lời cho câu hỏi của riêng bạn. – rdelmar

+0

Nhưng làm cách nào để chọn nguyên mẫu nào áp dụng cho hàng nào? –

Trả lời

16

Nếu bạn đang sử dụng ba nguyên mẫu thì hãy sử dụng ba số nhận dạng. Bởi vì chỉ một định danh sẽ gây ra vấn đề. Và bạn sẽ nhận được kết quả sai. Vì vậy, mã như thế này.

if(indexPath.row==0){ 
// Create first cell 
} 

if(indexPath.row==1){ 
// Create second cell 
} 

else{ 
// Create all others 
} 

Bạn cũng có thể sử dụng vỏ chuyển đổi ở đây để có hiệu suất tốt nhất.

+0

Trong khi tạo ô, bạn có thể làm theo cùng một phương pháp, trước khi bạn đang sử dụng với số nhận dạng duy nhất. Nhưng ở đây bạn sẽ sử dụng ba định danh trong deque. –

3
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"]; 

    if (cell.tag == 0) 
    { 
    return array1.count; 
    } 
    else(cell.tag == 1) 
    { 
    return array2.count; 
    }  
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *cellIdentifier; 

NSString *membershipType = [membershipTypeArray objectAtIndex:indexPath.row]; 

if ([membershipType isEqualToString:@"silver"]||[membershipType isEqualToString:@"gold"]) 
{ 
    cellIdentifier = @"cell"; 
} 
else if ([membershipType isEqualToString:@"platinum"]) 
{ 
    cellIdentifier = @"premiumCustomCell"; 
    cell.customCellImageView.image = [cellImageArray objectAtIndex:indexPath.row]; 
} 

cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 

if (!cell) { 
    cell = [[CustomCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
} 
cell.selectionStyle = UITableViewCellSelectionStyleNone; 
cell.headingLabel.text = [titleArray objectAtIndex:indexPath.row]; 
} 
+0

cách xử lý không. các hàng trong phần. nếu bạn có 2 ô mẫu và không có hàng nào được hiển thị dựa trên số lượng mảng? –

+0

bạn chỉ cần gắn thẻ ô mẫu thử tùy chỉnh và kiểm tra số lượng mảng của bạn cho mỗi ô để trả về 'array.count' –

+0

@surajkthomas xem chỉnh sửa của tôi ở trên –

1

Ở đây tôi đã viết mã như:

#pragma mark == Tableview Datasource 

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

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
NSInteger nRows = 0; 
switch (section) { 
    case 0: 
     nRows = shipData.count; 
     break; 
    case 1: 
     nRows = dataArray1.count; 
     break; 
    default: 
     break; 
} 
return nRows; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
NSString *cellIdentifier = @"cellIdentifier1"; 
NSString *cellIdentifier1 = @"cellIdentifier2"; 
SingleShippingDetailsCell *cell; 
switch (indexPath.section) { 
    case 0: 
     cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
     //Load data in this prototype cell 
     break; 
    case 1: 
     cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier1]; 
     //Load data in this prototype cell 
     break; 
    default: 
     break; 
} 
return cell; 
} 
Các vấn đề liên quan