Tôi đang cố gắng tạo một loại accordion của uitableviewcell, khi người dùng chọn ô, nó sẽ mở rộng để hiển thị chế độ xem thông tin chi tiết nội tuyến tương tự như cách hoạt động của ứng dụng digg. Ban đầu tôi đã thử thay thế tablecell hiện tại bằng một customcell trong cellForRowAtIndex, tuy nhiên hoạt hình trông hơi bị thay đổi khi bạn có thể thấy ô được thay thế và hiệu quả tổng thể không hoạt động tốt.Tế bào bảng Accordion - Làm thế nào để tự động mở rộng/hợp đồng uitableviewcell?
Nếu bạn nhìn vào ứng dụng digg và những người khác đã làm điều này có vẻ như họ không thay thế tế bào hiện tại mà thay vào đó có lẽ thêm một subview vào tế bào? Tuy nhiên, ô ban đầu dường như không hoạt ảnh và chỉ có chế độ xem mới phù hợp với bảng.
Có ai có ý tưởng nào về cách thực hiện hiệu ứng tương tự không?
Cập nhật: Tôi đã thực hiện một số tiến bộ sử dụng phương pháp neha của dưới đây và trong khi tế bào được tạo hiệu ứng động một cách chính xác nó đang tàn phá với các tế bào khác trong bảng. Những gì tôi đã làm là subclassed UITableViewCell với một lớp tùy chỉnh trong đó có một thể hiện của một UIView mà thực sự rút ra cái nhìn mà tôi sau đó thêm vào các tế bào bảng contentview.
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
if (selected) {
[self expandCell];
}
}
-(void)expandCell {
self.contentView.frame = CGRectMake(0.0, 0.0, self.contentView.bounds.size.width, 110);
}
Dưới đây là tất cả các phương pháp đại biểu bảng tôi đang sử dụng:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
if (isSearching && indexPath.row == selectedIndex) {
static NSString *CellIdentifier = @"SearchCell";
CustomTableCell *cell = (CustomTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[cell setCustomTitle:[timeZoneNames objectAtIndex:indexPath.row] detail:[timeZoneNames objectAtIndex:indexPath.row]];
UILabel *theText = [[UILabel alloc] initWithFrame:CGRectMake(10.0, 10.0, cell.contentView.bounds.size.width -20, 22.0)];
theText.text = @"Title Text";
[cell.contentView addSubview:theText];
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10.0, 10 + 46.0, cell.contentView.bounds.size.width - 20, 40.0)];
textField.borderStyle = UITextBorderStyleLine;
[cell.contentView addSubview:textField];
UILabel *testLabel = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 88.0, cell.contentView.bounds.size.width - 20, 22.0)];
testLabel.text = [NSString stringWithFormat:@"Some text here"];
[cell.contentView addSubview:testLabel];
[theText release];
[textField release];
[testLabel release];
return cell;
} else {
static NSString *CellIdentifier = @"Cell";
CustomTableCell *cell = (CustomTableCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[CustomTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
[cell setCustomTitle:[timeZoneNames objectAtIndex:indexPath.row] detail:[timeZoneNames objectAtIndex:indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
selectedIndex = indexPath.row;
isSearching = YES;
[tableView beginUpdates];
[tableView endUpdates];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
if (isSearching && indexPath.row == selectedIndex) {
return 110;
}
return rowHeight;
}
Có vẻ như bây giờ mà các tế bào được mở rộng nhưng không thực sự được làm mới nên nhãn và textfield arent được hiển thị. Tuy nhiên, chúng hiển thị khi tôi di chuyển ô và tắt trên màn hình.
Bất kỳ ý tưởng nào?
ai? Không tìm kiếm mã chi tiết mà chỉ là một số hướng dẫn về cách đạt được điều này? –
Tại sao bạn không trả lời câu trả lời chính xác của Pawel ... Bạn không thể làm tốt hơn thế. –