2012-02-15 28 views
9

Tôi muốn xác định bảng xem tĩnh với một phần động Điều đó có thể?Bao gồm một phần có "nguyên mẫu động" trong một Bảng nhìn tĩnh với "ô tĩnh"

phần 0 phải tĩnh, các lab được nối bằng xcode với các cửa hàng.

phần 1 phải là động

Tôi đã thử điều này, nhưng tôi không biết mình sẽ trả về phần tĩnh nào.

static NSString *CellIdentifier = @"ItemCellBasic"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

switch (indexPath.section) 
{ case 0: 
    return // I don´t know what 

    case 1: 
    cell.textLabel [email protected]"dynamic"; 
    return cell;  
} 

EDIT 1; bây giờ tôi cố gắng:

case 0: return [super tableView:tableView cellForRowAtIndexPath:indexPath]; 

nhưng có:

*** Assertion failure in -[UITableView _createPreparedCellForGlobalRow:withIndexPath:], /SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:6072 
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:' 
+0

thuộc tính: "nội dung" trong xcode, whith có thể là "ô tĩnh" hoặc "nguyên mẫu động". – mica

Trả lời

5

tôi có một giải pháp phần cho vấn đề này

Trong Bảng xem phương pháp nguồn dữ liệu tôi trả lại superclasses kết quả cho các tế bào tĩnh , đối với các ô động, tôi trả về các giá trị động cần thiết.

Trong - (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath các dequeueReusableCellWithIdentifier: trả về Nil vì vậy tôi tạo ra một UITableViewCell mới

Vấn đề còn lại:

trong xcode bạn phải chỉ định, số hàng trong "phần động" (whitch là ofcourse không năng động). Bạn không thể hiển thị nhiều hơn mức tối đa bạn đã xác định tại đây (hoặc nhận ngoại lệ ;-)).

Samplecode:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    switch (section) 
    { case STATIC_SECTION: 
     return [super tableView:tableView numberOfRowsInSection:section]; 

    case DYNAMIC_SECTION 
     return NUMBER_OF_DYNAMIC_ROWS; 
    } 
} 

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

    switch (indexPath.section) 
    { 
    case STATIC_SECTION: 
     return [super tableView:tableView cellForRowAtIndexPath:indexPath]; 

    case DYNAMIC_SECTION: 
     cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
     if (!cell) 
     { cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ; 
     } 

     cell.textLabel.text=DYNAMIC_TEXT; 
     return cell;  
    } 

} 
+0

Thực ra bạn có thể thêm bất kỳ số lượng ô nào. Xem [link] (http://stackoverflow.com/questions/10043521/adding-unknown-number-of-rows-to-static-cells-uitableview/10060997#comment15940351_10060997) – DanSkeel

-1

thay vì một công tắc hãy thử sử dụng một số tốt cũ nếu phát biểu

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
     if(section==STATIC_SECTION){ 
      return *the number of rows in the static section* 
     } 
     else{ 
      return NUMBER_OF_DYNAMIC_ROWS; 
     } 
    } 

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

    static NSString *CellIdentifier = @"ItemCellBasic"; 
    UITableViewCell *yourCell = [tableView dequeReusableCellWithIdentifier:CellIdentifier forIndexpath:indexPath]; 

    yourCell.text = *your dynamic text or something* 

    return yourCell; 
} 

tại giả định các tế bào tĩnh của bạn không có định tái sử dụng, nguyên nhân đó sẽ chỉ được thừa, và vì bạn chỉ cần một ô mẫu để tái sử dụng nên thiết lập của bạn là

Các vấn đề liên quan