Reputation: 4220
I am trying to add a customCell which simply will allow me to left align one string, and right align the other. In a previous question someone had suggested the following, but I am having problems getting it to work with my code. Xcode says: RootviewController may not respond to --contentView, and that call [[self contentView] addSubView:item] or [[self contentView] addSubView:rank] will crash my app at runtime
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
UILabel *rank = [[UILabel alloc] initWithFrame:CGRectMake(5, 5, 100, 20];
//Mess around with the rects, I am just merely guessing.
[rank setTag:5];
[[self contentView] addSubView:rank];
[rank release];
UILabel *item = [[UILabel alloc] initWithFrame:CGRectMake(110, 5, 220, 20];
//Important
[item setTextAlignment:UITextAlignmentRight];
[item setTag:6];
[[self contentView] addSubView:item];
[item release];
}
UILabel *rank = (UILabel *)[cell viewWithTag:5];
UILabel *item = (UILabel *)[cell viewWithTag:6];
rank = @"leftside";
item = @"rightside";
}
Thanks for any ideas
Upvotes: 0
Views: 150
Reputation: 12325
You should be looking at
[cell.contentView addSubview:item];
[cell.contentView addSubview:rank];
rank.text = @"leftside";
item.text = @"rightside";
One more thing to note here. If your UITableView is scrollEnabled
, you will have problems with cellReusability
and your labels will get messed up with subsequent scrolls. I would suggest that you subclass UITableViewCells and add those in the layout, and then use the CustomUITableViewCells
.
Upvotes: 1
Reputation: 15597
self
here is the controller. Instead of making self
the receiver, you should be sending contentView
to the cell you just created.
Upvotes: 1